Syntax sugar for {} with structs
Nick Treleaven
nick at geany.org
Sat Jul 2 11:26:23 UTC 2022
On Friday, 1 July 2022 at 20:21:45 UTC, Max Samukha wrote:
> On Friday, 1 July 2022 at 19:03:25 UTC, Paul Backus wrote:
>
>>
>> ```d
>> struct A {}
>> struct B {}
>>
>> void f(A) {}
>> void f(B) {}
>>
>> f({}); // ambiguous
>
> Why is this more problematic than e.g.:
>
> ```d
> void f(int[])
> {
> }
>
> void f(byte[])
> {
> }
>
> void main()
> {
> f([]); // Nice ambiguity error
> }
> ```
> ?
`[]` has type `noreturn[]` [1], so the conversion to other array
types will work naturally through the type system. `{}` would
need special casing outside the type system, or a new type
created just for it. (Assuming `{}` as an empty function literal
was deprecated).
[1]
https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md#the-type-of-the-empty-array-literal
More information about the Digitalmars-d
mailing list