March 01, 2022
On 3/1/22 07:19, Mike Parker wrote:
> On Tuesday, 1 March 2022 at 13:15:09 UTC, meta wrote:
>
>>
>>     enum Color
>>     { GRAY }
>>
>>     void setColor(Color color);
>>
>>     setColor(GRAY);
>
> Then that defeats the purpose of having named enums.

Yes and no.

meta is pointing at a difference between the above and the following:

  writeln(GRAY);

In the latter case, the compiler has no clue whether I intended to type GRAM. But in the former case, the type is Color. What remains is whether the compiler should be looking deep into Color and have a list of values to lower GRAY to Color.GRAY.

We heard this before for the switch statement: When the variable is Color, the case values can be accepted as Color as well (without the qualifier). (Yes, 'with' works as well, but the idea is the same.)

It feels the same for even int because we don't write int(42) when passing an int argument:

void foo(int) {}

foo(int(42));  // works
foo(42);       // works as well

So the lack of this compiler help does not bother me but still, I think the request is meaningful.

Ali

March 01, 2022

On Monday, 28 February 2022 at 12:18:37 UTC, Mike Parker wrote:

>

Then you can mixin aliases for any named enum members you'd like:

mixin(expandEnum!Colors);

Meanwhile it's very skillful :)

It is possible to change all the color palette with a second parameter:

import std.stdio;

enum Colors { BLACK, GRAY, WHITE }
enum Color {  WHITE, GRAY, BLACK }

mixin(expandEnum!(Colors, "Color"));

void main() {
  with(Colors) writefln("%d%d", GRAY, WHITE);// 12

  writefln("%d%d", GRAY, WHITE);  // 10
}
March 03, 2022
On Tuesday, 1 March 2022 at 15:37:55 UTC, Ali Çehreli wrote:
> On 3/1/22 07:19, Mike Parker wrote:
> > On Tuesday, 1 March 2022 at 13:15:09 UTC, meta wrote:
> >
> >>
> >>     enum Color
> >>     { GRAY }
> >>
> >>     void setColor(Color color);
> >>
> >>     setColor(GRAY);
> >
> > Then that defeats the purpose of having named enums.
>
> Yes and no.
>
> meta is pointing at a difference between the above and the following:
>
>   writeln(GRAY);
>
> In the latter case, the compiler has no clue whether I intended to type GRAM. But in the former case, the type is Color. What remains is whether the compiler should be looking deep into Color and have a list of values to lower GRAY to Color.GRAY.
>
> We heard this before for the switch statement: When the variable is Color, the case values can be accepted as Color as well (without the qualifier). (Yes, 'with' works as well, but the idea is the same.)
>
> It feels the same for even int because we don't write int(42) when passing an int argument:
>
> void foo(int) {}
>
> foo(int(42));  // works
> foo(42);       // works as well
>
> So the lack of this compiler help does not bother me but still, I think the request is meaningful.
>
> Ali

Yes, that's exactly the point I was trying to make, thanks Ali!
1 2
Next ›   Last »