I'm not sure if I'm confused, or if there's a problem here, and I don't 
know what any fix would be anyway, so apologies for the open-ended post 
but...

I cannot find on "option" type in Julia that I can dispatch on, so that I 
have a method call different functions, depending on whether a value is 
present or not.

What I think I need is:

typealias Maybe{T} Union(T,Nothing}



That allows me to do:

julia> foo(::Int) = "int"
foo (generic function with 1 method

julia> foo(::Nothing) = "nothing"
foo (generic function with 2 methods)

julia> bar(x::Maybe{Int}) = foo(x)
bar (generic function with 1 method)

julia> foo(1)
"int"

julia> foo(nothing)
"nothing"

julia> bar(nothing)
"nothing"

julia> bar(1)
"int"


which seems like what I want.

HOWEVER, there's also Nullable, described at 
http://julia.readthedocs.org/en/latest/manual/types/#nullable-types-representing-missing-values
 
and also at 
http://docs.julialang.org/en/latest/manual/faq/#nothingness-and-missing-values 
which seems like what I "should" be using.  But I can't see how to dispatch 
on it.

So it seems like one of the following is true

1 - There is a way to dispatch on Nothing, and please someone explain it to 
me

2 - Nullable should be changed so that it can be dispatched on

3 - We need Maybe in Base as well as Nothing.

1 or 2 sounds fine (although I personally don't see how you can do 2).  3 
seems like the worst option, but still is presumably better than everyone 
and their dog defining this type in their own code.

Any enlightenment appreciated,
Andrew

PS More references

https://github.com/JuliaLang/julia/issues/3332

https://github.com/JuliaLang/julia/issues/1134

https://github.com/JuliaLang/julia/pull/8152

Reply via email to