Syntax sugar for {} with structs

Ola Fosheim Grøstad ola.fosheim.grostad at gmail.com
Sat Jul 2 13:59:39 UTC 2022


On Saturday, 2 July 2022 at 11:26:23 UTC, Nick Treleaven wrote:
> `{}` would need special casing outside the type system, or a 
> new type created just for it. (Assuming `{}` as an empty 
> function literal was deprecated).

How it works in C++ is that it is a list for the constructor, so 
if D wants to do this then the compiler should type {} as a 
parameter list.

E.g. in C++ it works like this:

```C++
tuple<int,double,tuple<float,string>> f() {
     return {0,1.0,{3.14f,"hello"}};
}

```

Or

```C++
using A = tuple<int,double,tuple<float,string>>;

void f(A x){}

int main()
{
     f({0,1.0,{3.14f,"hello"}});
}
```

Ambiguities are not a big deal, just prefix with the type:

```C++
f(A{0,1.0,{3.14f,"hello"}});
```




More information about the Digitalmars-d mailing list