Syntax sugar for {} with structs

ryuukk_ ryuukk.dev at gmail.com
Fri Jul 1 19:18:27 UTC 2022


>
> ```d
> struct A {}
> struct B {}
>
> void f(A) {}
> void f(B) {}
>
> f({}); // ambiguous
> ```


     Error: function overload found, ambiguous, please be explicit

> ```d
> void g(A) {}
> void g(int) {}
>
> g({}); // not ambiguous, but potentially confusing
> ```


     Error: function overload found, ambiguous, please be explicit


> ```d
> void h(A, B) {}
>
> h({}, {}); // not ambiguous, but potentially confusing
> ```

I do not see any confusions here


> The obvious solution is to make ambiguous usage a compile-time 
> error. However, even if you do that, there are still the 
> unambiguous-but-confusing cases to deal with.

Ok we thought of the same about the error, that is nice to read, 
i still believe the confusion is over estimated

```d
stuff(byte, int);
stuff(1, 1215145415);
```

Why it's not a confusion here?, we don't do things like this:

```d
stuff(byte(1), int(1215145415));
```


> Another solution is to narrow the scope of the proposal, and 
> just make `= {}` a special-case syntax for assignment, without 
> allowing the use of `{}` anywhere else. The problem with this 
> proposal is that you can get exactly the same result using the 
> utility function, so the benefits are probably not high enough 
> to outweigh the fixed costs of adding a new language feature.

If compile error for ambiguous i don't think limiting it to just 
that is right, it'd make things inconsistent imo






More information about the Digitalmars-d mailing list