> might consider supporting BAZ=20 syntax
agreed.
> Should these be defined in a module so that they are Foo.BAR, etc.?
Perhaps an even better solution to this would be to allow either
(a) dispatch on a module, as a type, so we could write both f(::Foo) and Foo.BAR
(b) overloading the dot operator for the same result, minus the
ability to write `import Foo.BAR` (although `const BAR = Foo.BAR`
should still work)
I think (a) could be really cool, if it was feasible.
> both a numeric value and a (sub-)type that can be dispatched on
I still think that misses the point of an enum. If I want to dispatch
on a type, I would just make a type:
type Fruit{T}
Fruit() = (T in [:Apple, :Banana] ? new() : error("invalid enum
value for Fruit")
end
function peel_generic(f::Fruit, ...) ...
function eat_apple(::Fruit{:Apple}, ...) ...
function eat_banana(::Fruit{:Banana},...) ...
If I really want them to be numbered, that's also pretty easy to arrange:
let
local const options = [:Apple, :Banana]
global Fruit
type Fruit{T}
Fruit() = (T in options ? new() : error("invalid enum value for Fruit")
end
for (i,T) in enumerate(options)
@eval Base.convert{I}(::Type{I}, ::Fruit{$T}) = $i
end
Base.convert(::Type{Fruit}, i::Integer) = Fruit{options[i]}
end
Also valid (and basically the same) is an abstract type:
abstract Foo
type Bar <: Foo end
etc.
On Thu, Dec 26, 2013 at 11:02 PM, Stefan Karpinski
<[email protected]> wrote:
> That was sort of what I was getting at, but I think it's overkill (not to
> mention not possible without significant additional complications).
>
> On Dec 26, 2013, at 10:01 PM, andrew cooke <[email protected]> wrote:
>
> i still don't get how / when julia's types are dependent, but would it be
> possible to make enums so that you could have something like:
>
>
> @enum Fruit APPLE BANANA
>
> function peel_generic(f::Fruit, ...) ...
> function eat_apple(::Fruit{APPLE}, ...) ...
> function eat_banana(::Fruit{BANANA},...) ...
>
> in other words, some way to get the best of both worlds - both a numeric
> value and a (sub-)type that can be dispatched on? i'm thinking that
> Fruit{APPLE} is a bit like Array{..., 1} hence my mentioning dependent
> types.
>
> i hope that's clear. i suspect i am not using the right words and asking
> for the impossible...
>
> andrew