October 07, 2010
On 10/7/10 3:55 CDT, Pelle wrote:
> On 10/07/2010 08:08 AM, Walter Bright wrote:
>> If expr represents a tuple, we (Andrei and I) were thinking about the
>> syntax:
>>
>> auto (a, b, c, d) = expr;
>>
>> being equivalent to:
>>
>> auto t = expr; auto a = t[0]; auto b = t[1]; auto c = t[2 .. $];
>>
>> You can also do this with arrays, such that:
>>
>> float[3] xyz;
>> auto (x, y, z) = xyz;
>>
>> The Lithpers among you will notice that this essentially provides a
>> handy car,cdr shortcut for tuples and arrays:
>>
>> auto (car, cdr) = expr;
>
> Python 3 uses:
> car, *cdr = expr
> a, *b, c = [1,2,3,4,5] # leaves a=1, b=[2,3,4], c=5
>
> I would like D to have
> (car, cdr...) = expr
> (a, b..., c) = [1,2,3,4,5]
>
> for the equivalent.
>
> Our varargs syntax is b..., theirs is *b. So it mirrors a bit, there. :-)

Excellent idea!

Andrei
October 07, 2010
Andrei Alexandrescu, el  7 de octubre a las 03:20 me escribiste:
> On 10/7/10 1:43 CDT, Russel Winder wrote:
> >On Wed, 2010-10-06 at 23:08 -0700, Walter Bright wrote:
> >>If expr represents a tuple, we (Andrei and I) were thinking about the syntax:
> >>
> >>      auto (a, b, c, d) = expr;
> >>
> >>being equivalent to:
> >>
> >>      auto t = expr; auto a = t[0]; auto b = t[1]; auto c = t[2 .. $];

I guess d being missing is a typo, right?

> >>You can also do this with arrays, such that:
> >>
> >>      float[3] xyz;
> >>      auto (x, y, z) = xyz;
> >>
> >>The Lithpers among you will notice that this essentially provides a handy car,cdr shortcut for tuples and arrays:
> >>
> >>      auto (car, cdr) = expr;
> >
> >
> >Python may be the best base to compare things to as tuple assignment has been in there for years.
> >
> >Pythons choice is not a car/cdr approach but an exact match approach.
> 
> So then we'd have the proposed notation not work with dynamic arrays - only with static arrays and tuples.

Unless you add a dynamic "bound" check as when accessing a dynamic array item, something like:

auto t = expr; assert (t.lenght == 4); auto a = t[0]; auto b = t[1];
	auto c = t[2]; auto d = t[3];

I like the idea of having exact match approach and the explicit syntax for getting the rest as Brad said. But in all the years I used Python, I never needed that syntax, maybe because most of the times when I use the tuple expansion I know the size or I want to truncate, or I use something to generate the data, like split(), that takes an extra parameter to do that:

l = [1, 2, 3]
a, b, c = l                  # known lenght
a, b = l[:2]                 # truncation (like l[0..2] in D)
a, b = '1,2,3'.split(',', 1) # get the rest in b (but it will be a string)
car, cdr = l[0], l[1:]       # just a little more verbose

-- 
Leandro Lucarella (AKA luca)                     http://llucax.com.ar/
----------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------
careful to all animals (never washing spiders down the plughole),
keep in contact with old friends (enjoy a drink now and then),
will frequently check credit at (moral) bank (hole in the wall),
October 07, 2010
Walter Bright napisał:

> If expr represents a tuple, we (Andrei and I) were thinking about the
> syntax:
> 
>      auto (a, b, c, d) = expr;
> 
> being equivalent to:
> 
>      auto t = expr; auto a = t[0]; auto b = t[1]; auto c = t[2 .. $];

Typo? If not, what is 'd'?
Either way, I'd like mismatching tuple lengths to fail, not assign the tail to the last variable.
Or, as pelle brought up: auto (a, b..., c) = expr, where b = expr[1..2] and you may have
only one ... in the lhs. It's not bad.

> You can also do this with arrays, such that:
> 
>      float[3] xyz;
>      auto (x, y, z) = xyz;
>
> The Lithpers among you will notice that this essentially provides a handy car,cdr shortcut for tuples and arrays:
> 
>      auto (car, cdr) = expr;

Nice. It's all nice but as my colleague once said: put it on the todo list right after 'learn Portugese'.

-- 
Tomek
1 2
Next ›   Last »