Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

How to dispatch by value?

$
0
0

Very beginner question about using “where” in function declaration.

Is it possible to do something like this?

function p(c::Structure) where c.id == :x   
    do something now I know c.id == :x
end
function p(c::Structure) where c.id == :y 
    do something now I know c.id == :y
end

Of course I did try and no it can’t be done… so basically asking what can be done to emulate multiple dispatch by value.

The following is obvious and trivial BUT I’d like to modularize by putting each function f in its own file, which can’t be done by this monolithic approach.

function p(c::Struct)
    if c.id == :x 
        do something
    elseif c.id == :y
        do something else
    end
end

Defining Structure as abstract, and have subtypes like

StructureX <: Structure
StructureY <: Structure
...

will need a lot of such subtypes in my intended use case.

17 posts - 8 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles