Thoughts on possible tuple semantics

bearophile bearophileHUGS at lycos.com
Fri Aug 23 09:34:11 PDT 2013


H. S. Teoh:

> any indexable range can be supported by this syntax. In
>   fact, I'd argue that you should be able to do this even with 
> just an
>   input range:
>
> 	auto (x, y, z) = makeInputRange();
>
>   should be translated into:
>
> 	auto tmp = makeInputRange();
> 	assert(!tmp.empty);
> 	auto x = tmp.front;
> 	tmp.popFront();
> 	assert(!tmp.empty);
> 	auto y = tmp.front;
> 	tmp.popFront();
> 	assert(!tmp.empty);
> 	auto z = tmp.front;
>
>   Since ranges are a major selling feature of D, I'd argue that
>   in-language support should be completely appropriate, and even
>   desirable.

Yes, this is an "obvious" nice feature to support, I didn't list 
it because I wasn't bold enough :-)

This is a good situation to show how other languages do, this is 
Python2:

>>> lazy = (x * x for x in xrange(1, 4))
>>> lazy
<generator object <genexpr> at 0x02230238>
>>> a, b, c = lazy
>>> a
1
>>> b
4
>>> c
9

Similar code is possible in Haskell and Perl6.

Bye,
bearophile


More information about the Digitalmars-d mailing list