Type Inference for Struct/Enum Literals
ryuukk_
ryuukk.dev at gmail.com
Sat Jul 6 08:04:36 UTC 2024
On Saturday, 6 July 2024 at 02:49:32 UTC, Steven Schveighoffer
wrote:
> On Friday, 5 July 2024 at 13:19:44 UTC, IchorDev wrote:
>
>> - The `.` syntax feels natural for enum literals, but not for
>> struct literals. This would have to mean changing the module
>> scope operator (perhaps to `./`? (Like to how `./` represents
>> 'here' in a terminal) because otherwise module-level symbols
>> would interfere.
>> - My original proposal used `$`, which I still like for struct
>> literals, but it's a bit random.
>> - I don't like the C initialiser syntax (`{}`) but it could be
>> re-purposed for type-inferred struct literals, which would
>> mean we can basically merge the two.
>
> OK, so in fact, we have an interesting possibility here. With
> editions we *could* potentially overtake the `.` prefix, and
> use something else for global scope.
>
> The biggest problem with this is -- it breaks all existing
> knowledge of what this does. It is, for sure, the most natural
> syntax for this feature. One might argue that `$.symbol` could
> mean "global symbol" instead (or something else).
>
> It's a pretty big lift for this, and I don't see it being
> accepted. Already there is pushback from the language
> maintainers on the concept itself (from the original review).
>
> However, I would love to see this inference piece get into the
> language, whatever the syntax. I very much enjoyed it in my
> swift programs. I would guess that the best possibility to be
> accepted would be to have syntax that doesn't conflict with
> existing syntax.
>
> -Steve
The pushback is from people who never got to learn new languages,
they believe this is what everyone wants to write
`MySelfExplanatoryType flag = MySelfExplanatoryType.A |
MySelfExplanatoryType.B MySelfExplanatoryType.C;`
And when they tell you that "you can use an alias", it's when you
know they are being dishonest, the point is not to make things
unreadable or obfuscated, it's to avoid repetition and to make
code more concise in places that are relevant
I use many languages, the ones that i want to stick with the most
are the ones that lets me write concise code, recently it's been
Odin for me
`change_state(State.IDLE);`
There is no reason to repeat "state" in that line
same here:
```D
switch (player_state)
{
case State.IDLE: /* .. */ break;
}
```
```D
import std.stdio;
import module_a.module_b.module_c;
void main() {
module_a.module_b.module_c.Data data;
data.value = int(42);
print_data(data);
module_a.module_b.module_c.this_is_a_function(module_a.module_b.module_c.MyEnum.A);
}
void print_data(module_a.module_b.module_c.Data data) {
std.stdio.writeln(data);
}
```
Now you understand the value of concise code ;)
I think rikki proposed `:`, perhaps that's the way
More information about the dip.ideas
mailing list