enum inheritance

JS js.mdnq at gmail.com
Mon Jul 15 15:44:28 PDT 2013


On Monday, 15 July 2013 at 19:24:27 UTC, Dicebot wrote:
> On Monday, 15 July 2013 at 18:26:26 UTC, JS wrote:
>> Original I had it as a class. I'm not sure if it matters much 
>> between a class and a struct though?
>
> It does matter a lot. structs are value types, classes are 
> polymorphic reference types. There is a nice summary table in 
> docs: http://dlang.org/struct.html , please notice that 
> "inheritance" and this "final" applies only for classes.
>
um, but I'm only using the type at compile time. the immutable 
fields in it all result in compile time literals... the struct 
itself never gets used at run time... so there is no runtime 
difference(or shouldn't be) except maybe a little more wasted 
space with the class unless it is optimized out.


>> In any case, I solved this problem by using an attribute to 
>> test instead of using isMutable. Obviously this requires 
>> adding a symbol but not a huge deal.
>>
>> e.g.,
>>
>> @Enum immutable struct ...
>>
>> and hasAttribute(T, Enum) replaces isMutable(T).
>
> Yeah, that may work, despite being not that pretty. I 
> personally think it is better for now chose some 
> not-that-elegant approach in your code, at least until enums 
> will become more robust. Currently it tries to workaround some 
> very basic type system assumptions, which rarely ends good.

I really don't have 10 years to wait for enums to become more 
robust and hope in the way I am trying to use them.


More information about the Digitalmars-d-learn mailing list