Classes as enums in D?
Rikki Cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 30 00:09:50 PST 2015
On 30/11/15 8:58 PM, Andrew LaChance wrote:
> On Monday, 30 November 2015 at 07:54:49 UTC, Rikki Cattermole wrote:
>> enums don't have to be integral, but for performance reasons it is for
>> the best.
>>
>> enum Foo : string {
>> A = "a",
>> B = "b",
>> C = "d",
>> ERROR = "What are you talking about?"
>> }
>>
>> void main() {
>> import std.stdio : writeln;
>> Foo foo = Foo.ERROR;
>> writeln(foo, " is: ", cast(string)foo);
>> }
>>
>> Also you are welcome in #d on Freenode (IRC) if you are interesting in
>> talking with the rest of us!
>> Btw you probably want tuples (std.typecons : tuple) to emulate those
>> values.
>
> Oh interesting. So you are saying I could have a struct WhiteKey {...}
> and then an enum that extends WhiteKey? You also mention Tuples, are
> you recommending the Tuple's type be (WhiteKey, int)? Thanks!
An enum does not extend other types.
It specifies what the type of the value will be.
It is a little like a map in that way.
I would recommend that the tuple to be the value type of the enum.
You could alias TypeTuple and use it directly and name the fields.
All it is, is a struct.
More information about the Digitalmars-d-learn
mailing list