version(StdDoc)

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Nov 26 17:51:52 UTC 2018


On Sun, Nov 25, 2018 at 09:53:50PM +0000, Stanislav Blinov via Digitalmars-d-learn wrote:
> 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

I dunno, given that D structs are supposed to be "glorified ints"
according to TDPL, I see enum declarations more-or-less as a shorthand
for structs of the above sort. :-D  Much like template functions / enums
/ etc. are shorthands for eponymous templates.


> > 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.
[...]

Haha, didn't know that.


T

-- 
Dogs have owners ... cats have staff. -- Krista Casada


More information about the Digitalmars-d-learn mailing list