uniform initialization in D (as in C++11): i{...}

ZombineDev via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 5 07:45:33 PDT 2016


On Tuesday, 5 April 2016 at 11:35:35 UTC, Jacob Carlborg wrote:
> On 2016-04-05 09:47, ZombineDev wrote:
>
>> D currently supports
>> Point3 p = { x:1, y:2, z:3 };
>>
>> It just needs to be extended to work in more places.
>>
>> https://issues.dlang.org/show_bug.cgi?id=15692
>
> That's only for structs. A uniform initialization syntax needs 
> to be ... uniform. It has to work for everything.

Yeah, that's what I had in mind. new Object{}, int(3) -> int{3}, 
T() -> T{} (in generic code), and {42, "tuple"} where the type 
can be deduced.

On the other hand, this may not be such a good idea, because of 
https://github.com/D-Programming-Language/dmd/pull/1356, which 
made T() the standard syntax for uniform construction syntax, 
instead of T{}. Unfortunately the (1, 2, 3) syntax is can't be 
used for tuples because it is ambiguous/looks bad in some places:
// this looks ok
auto tuple = (1, 3.4, "a string");
// however this looks really strange
foo((1, 3.4, "a string"))
// and this even more ambiguous for the reader.
int a=1, b=2, c=3;
auto i = (a += 2, a + b); // C-style comma operator usage, or 
tuple?

Even the [] array syntax is not a good idea, because it implies a 
homogeneous container.

I guess this leaves binary operator abuse like:
auto tuple = |a, "asd"|;
auto tuple = <"asd, 42, 'c'>;
:D

IMHO, the curly brace syntax auto t = {'a', "tuple"}; is the 
least bad syntax.


More information about the Digitalmars-d mailing list