DIPX: Enum Literals / Implicit Selector Expression

Dom Disc dominikus at scherkl.de
Fri Jul 1 08:49:32 UTC 2022


On Thursday, 30 June 2022 at 20:32:49 UTC, ryuukk_ wrote:
> You don't do:
>
>     with(int) set_my_int(5 + 6 + 7 + 8 + 9 +10);

It's funny that in fact I like this really. It's much more 
readable than casting to the desired type. E.g. you might have 
wished every literal to be float:

with(float) foo(5 + 6 + 7 + 8 + 9 + 10);

Unfortunately that doesn't work, so we have to live with

foo(cast(float)5 + 6 + 7 + 8 + 9 + 10);

or

foo(5.0f + 6 + 7 + 8 + 9 + 10);

which looks short, but you don't know if the compiler will use 
float or double or real because of the strange propagation rules.

or cast every literal:

foo(5.0f + 6.0f + 7.0f + 8.0f + 9.0f + 10.0f);

Much worse if you want byte:

foo(cast(byte)5 + 6 + 7 + 8 + 250);

calls foo with int :-(
Here even casting each literal doesn't work, you have to cast the 
result. I really wish

with(byte) foo(5 + 6 + 7 + 8 + 9 + 10);

would work.


More information about the Digitalmars-d mailing list