What is the stance on partial initializers when declaring multiplevariables

bearophile bearophileHUGS at lycos.com
Fri Jul 22 10:32:49 PDT 2011


Andrei:

> Sounds great. The way Walter and I wanted to spec that would be as a 
> simple syntactic rewrite. This construct:
> 
> auto (a1, a2, ..., aN) = x;
> 
> is expanded mechanically to:
> 
> static assert(x.length == N, "Too many elements in definition");
> auto a1 = x[0];
> auto a2 = x[1];
> ...
> auto aN = x[N-1];
> 
> This ensures that the feature works with statically-sized arrays and 
> std.Tuple, but also with other types that define the required 
> operations. There's no special casing of library artifacts.
> 
> If x is an expression, it must be evaluated only once.
> 
> Let's discuss things here and hear from Walter before you embark on 
> doing this.

I have partially lost my grasp on this thread, but this seems what I was asking for. Plus making the syntax work for fixed sized arrays too (and similar user defined types) is good. I sometimes use 2-array or 3-array to return data by value when they are of the same type.

Two questions: is typed unpacking too supported?
(int a1, float a2, auto a3) = sometuple;

Is unpaking inside foreach too supposed to work? This is quite useful:
auto somepairs = [tuple(1, "foo"), tuple(10, "bar")];
foreach ((x, name); somepairs) {}

-----------

In Haskell you are allowed to call a function with a tuple, and give a local name to the tuple items:
somefunction (item1, item2)
and to optionally give a name to the whole tuple too:
somefunction tuplename@(item1, item2)
This is sometimes handy, but I think it's currently overkill in D (and it's an additive change).

Bye,
bearophile


More information about the Digitalmars-d mailing list