Julia cannot dispatch on values, but it can dispatch on types
parametrized by values. The canonical way of doing this is value types
(see in the Types section of the manual), but pretty much any other
parametrized type would work:
carsetup(::Type{Val{:fast}}) = "fast"
carsetup(::Type{Val{:slow}}) = "slow"
carsetup(Val{:fast}) # => "fast"
HTH,
Tamas
On Wed, May 13 2015, Elburz Sorkhabi <[email protected]> wrote:
> I was wondering if I could make a constructor function that works with
> multiple dispatch. My goal is to have a bool that can be set to true or
> false on creation as an argument, which would then change the way the
> constructor works. I could do it with an if statement, but I was curious
> about the more Julian way to approach it. I believe I'm looking for outer
> constructors, but I couldn't seem to find a good example, and the wiki is a
> little terse :) Below is my Python brain at work, but I feel like outer
> constructors using multiple dispatch should be how I approach it. Thanks!
>
> type Car
> fast::Bool
>
> function Car(fast::Bool)
> if fast == true
> do fast car specific setup
> else
> do slow car specific setup
> end
>
> do general car setup that both cars need
> end
> end