I am thinking about making an Enum type MyEnum, but each MyEnum is a composite immutable type. Is that possible (recommended) and how could I do that?
I've looked at - https://github.com/JuliaLang/julia/pull/10168 - And the @enum section of Docs but it still isn't obvious to me yet how to do it or if it is even possible/recommended. In psuedo-code, I'd like something like this: @enum MyEnum enum1 enum2 enum3 but each enum[i] is a composite immutable type. How would I construct them? I thought of something like: immutable MyEnum field1::Type1 filed2::Type2 end @enum MyEnum enum1 = MyEnum(f11,f12) enum2 = MyEnum(f21,f22) but I don't think that will work. Any ideas? Thank you. On Tuesday, January 27, 2015 at 4:55:18 AM UTC+8, Reid Atcheson wrote: > > Ok now that you have put it there, the comments in the documentation make > more sense to me. It looks like both yours and mine are essentially > equivalent, but yours is simpler. I was aiming for the following behavior > with my implementation: > > - different enum types won't typecheck (can't do "if e1::EnumType1 == > e2::EnumType2" without error). > - Array of enum type values contiguous in memory, for easy passing to C. > (immutable should do this) > - referring to enum fields by name, not by their numbering. > > I will switch to what you have written, it looks like it hits all of my > points while not simultaneously abusing the type system. > > On Monday, January 26, 2015 at 2:16:33 PM UTC-6, Mauro wrote: >> >> There is a FAQ entry on this which suggests not to use types each of >> elements of the enum (if I recall correctly). >> >> I recently did a enum like this: >> >> export nonstiff, mildlystiff, stiff >> abstract Enum >> immutable Stiff <: Enum >> val::Int >> function Stiff(i::Integer) >> @assert 0<=i<=2 >> new(i) >> end >> end >> const nonstiff = Stiff(0) >> const mildlystiff = Stiff(1) >> const stiff = Stiff(2) >> >> Then you can just do >> if someflag==nonstiff >> do_something >> end >> >> So no need either to refer to the numeral value. But I'm not sure >> whether this is better or not. >> >> On Mon, 2015-01-26 at 19:49, Reid Atcheson <[email protected]> wrote: >> > Hey all. I have frequently been in the position of wanting enumerations >> in >> > Julia. I have finally settled on the implementation linked below which >> lets >> > me refer to flags >> > in a named way and only specify their underlying numbering once. Is >> this >> > the best way, or are there better ways I haven't figured out? >> > >> > https://github.com/ReidAtcheson/EnumsInJulia >> >>
