Type Inference for Struct/Enum Literals
IchorDev
zxinsworld at gmail.com
Sat Jul 6 11:14:56 UTC 2024
On Saturday, 6 July 2024 at 08:04:36 UTC, ryuukk_ wrote:
> 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
Hey, precisely. :)
> ```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);
> }
> ```
That's a very amusing example. I never thought of how much more
verbose D could be without its syntactic sugar. You could've
added some range copying and eponymous templates in there for
good measure. ;)
With D being a reasonably modern language and having a lot of
shortcuts for writing otherwise cumbersome things, I really
always expected it to have a shortcut for enum literals, since
enum literals are the sort of thing so often used in places where
specifying the type doesn't help readability at all.
> I think rikki proposed `:`, perhaps that's the way
I'm not sure if it's the most readable thing when compared to
just `.` for enum literals, but it's certainly a decent option if
we're against replacing the module scope operator. For struct
literals it could work too. One thing is now that we have named
function parameters it might result in a bit of a colon headache:
`myFn(x: :a, y: cond ? :c : :d);`
More information about the dip.ideas
mailing list