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

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 5 09:18:37 PDT 2016


On Monday, April 04, 2016 22:39:25 Timothee Cour via Digitalmars-d wrote:
> what's D's answer for C++11's uniform initialization [1] which allows DRY
> code?
>
> Could we have this:
> ----
> struct A{
>   int a;
>   int b;
> }
>
> A fun(A a, int b) {
>   if(b==1) return i{0,1};
>   else if(b==2) return i{2,3};
>   else return fun(i{3,4}, 1);
> }
> ----

So, the whole point of this is to be able to construct the return value
without typing the type name explicitly? typeof(return) already does this.
So, this doesn't seem like it would be solving anything new, just making it
less verbose.

In general though, construction is one of those things that tends to not be
generic, so it frequently doesn't work to just swap out one type for another
when construction is involved, and construction frequently doesn't work very
well in templated code. When it does, the construction syntax is already
pretty generic, such that I don't see much need to add anythnig for it. We
just don't have anything specific for containers, but you're not going to
swap construction of a scalar value for a container, and if the containers
all took variadic lists of arguments and/or ranges, then swapping the
construction of one container to another would tend to work.

I really don't see a problem here that needs solving.

- Jonathan M Davis



More information about the Digitalmars-d mailing list