version(StdDoc)

Stanislav Blinov stanislav.blinov at gmail.com
Sun Nov 25 21:53:50 UTC 2018


On Sunday, 25 November 2018 at 21:38:43 UTC, H. S. Teoh wrote:

> Actually, I just thought of a way to do this with the existing 
> language: use a struct to simulate an enum:
>
> 	struct E {
> 		alias Basetype = int;
> 		Basetype impl;
> 		alias impl this;
>
> 		enum a = E(1);
> 		enum b = E(2);
> 		version(Windows) {
> 			enum c = E(3);
> 		}
> 		version(Posix) {
> 			enum c = E(4);
> 			enum d = E(100);
> 		}
> 	}

Heh, that can work in a pinch. Disgusting though :D

> It's not 100% the same thing, but gets pretty close, e.g., you 
> can reference enum values as E.a, E.b, you can declare 
> variables of type E and pass it to functions and it implicitly 
> converts to the base type, etc..
>
> There are some differences, like cast(E) won't work like an 
> enum...

It should, you can cast values of same sizeof to a struct.

> and .max has to be manually declared, etc.. You'll also need to 
> explicitly assign values to each member, but for OS-dependent 
> enums you have to do that already anyway.

Yeah, those aren't a huge concern for that particular scenario.


More information about the Digitalmars-d-learn mailing list