DIPX: Enum Literals / Implicit Selector Expression

Steven Schveighoffer schveiguy at gmail.com
Fri Jul 1 19:20:23 UTC 2022


On 7/1/22 2:22 PM, Walter Bright wrote:
> On 7/1/2022 8:42 AM, Steven Schveighoffer wrote:
>> Yes, but this is not exactly a case without precedent e.g.:
>>
>> ```d
>> short[] x = [1, 2, 3]; // short[]
>> auto y = [1, 2, 3]; // int[]
>>
>> void foo(short[] arr);
>> foo([1, 2, 3]); // works
>> ```
> 
> That's because the initializer, *after* it is evaluated for its type, 
> gets cast to the type of the lvalue.

Not really.

```d
auto a = [1, 2, 3];
short[] b = [1, 2, 3];
auto c = cast(short[])a;
writeln(b); // [1, 2, 3]
writeln(c); // [1, 0, 2, 0, 3, 0]
```

casting the literal is not the same as casting a regular value of that type.

What I am talking about is a thing that has not yet been given a type, 
and becomes the type you need if it's a valid use. Just like an array 
literal -- it's a complex expression that maintains it's polysemous 
quality. The only difference is, an enum `#value` would not have a 
default type.

-Steve


More information about the Digitalmars-d mailing list