The trouble is that values aren't usually know at compile time, which is why
dispatching on them doesn't work well.
When values of immutables can be known at compile time, something like your
proposal does work. For example, you can do this:
immutable Sentinel{S}
end
foo(::Type{Sentinel{:Case1}}) = println("1")
foo(::Type{Sentinel{:Case2}}) = println("2")
foo(Sentinel{:Case1})
foo(Sentinel{:Case2})
-- John
On Dec 16, 2014, at 10:10 AM, Luca Antiga <[email protected]> wrote:
> Dear Julia-dev,
> first of all, a big hat off for Julia. I've started experimenting with it
> and I'm very very impressed.
>
> While I was redesigning some code I had that I was porting to Julia, I kept
> reaching for dispatch based on a (constant) value of an argument, like
>
> foo(:a, arg1, arg2) -> do something
>
> foo(:b, arg1, arg2) -> do something else
>
> This is something one could achieve with Haskell's data constructors or
> Clojure's multimethods and it's convenient.
>
> I'm aware of PatternDispatch.jl, but maybe just allowing a type to be
> parameterized with a particular value, like Type{:a} (or ValueType{:a}, for
> instance), could fit well in Julia's design of the type system and would not
> really have any overhead.
>
> This would allow to write things like:
>
> function foo(s::Type{:a}, arg1, arg2)
> do_something()
> end
>
> function foo(s::Type{:b}, arg1, arg2)
> do_something_else()
> end
>
> Of course it's totally possible I'm not getting Julia's design right, so I
> apologize in advance.
>
> Thanks for such a great language, keep kicking!
>
>
> Luca
>