version(StdDoc)

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Nov 25 21:38:43 UTC 2018


On Sun, Nov 25, 2018 at 07:22:54AM +0000, Stanislav Blinov via Digitalmars-d-learn wrote:
> On Sunday, 25 November 2018 at 07:19:50 UTC, Stanislav Blinov wrote:
> 
> > Granted, it may require some special syntax, i.e.
> > 
> > enum E {
> >     a,
> >     b if version(Windows),
> >     c if version(Windows),
> >     d if version(Posix),
> > }
> > 
> > or something to that effect.
> 
> Come to think of it, since UDAs are now allowed, the compiler could
> potentially be taught this:
> 
> enum E {
>     a,
>     @version(Windows) b,
>     @version(Windows) c,
>     @version(Posix)   d,
> }

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);
		}
	}

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, 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.


T

-- 
Let's eat some disquits while we format the biskettes.


More information about the Digitalmars-d-learn mailing list