static alias this and if/do/while/for

Jack Applegame japplegame at gmail.com
Thu Oct 22 21:55:59 UTC 2020


There is a funny feature (or bug) in the D language:

static alias this and static operator overloading.

For example

> interface Foo {
>     static {
>         int value;
>         void opAssign(int v) { value = v; }
>         int get() { return value; }
>         alias get this;
>     }
> }

Now we can use type Foo as if it were an lvalue/rvalue:

> Foo = 5;
> int a = Foo;
> int b = Foo + a;

I heavily use this feature in my MCU library.

But it doesn't work inside conditionals:

> if(Foo) {}    // Error: type Foo is not an expression
> while(Foo) {} // Error: type Foo is not an expression

Even if we define `static opCast!bool()`. It doesn't help.

Should this be fixed?



More information about the Digitalmars-d-learn mailing list