Repost: make foreach(i, a; range) "just work"

Justin Whear justin at economicmodeling.com
Thu Feb 20 13:11:45 PST 2014


On Thu, 20 Feb 2014 19:34:17 +0000, w0rp wrote:

> On Thursday, 20 February 2014 at 16:30:42 UTC, Justin Whear wrote:
> 
>> Tuple unpacking already works in foreach.  This code has compiled since
>> at least 2.063.2:
>>
>> import std.stdio;
>> import std.range;
>> void main(string[] args)
>> {
>>     auto tuples = ["a", "b", "c"].zip(iota(0, 3));
>>
>>     // unpack the string into `s`, the integer into `i`
>>     foreach (s, i; tuples)
>>         writeln(s, ", ", i);
>> }
> 
> I did not know that. When did that happen? It didn't appear in any
> changelogs and it works when I tried it in 2.064 on my machine too. I
> suppose the next step after that would be to support nested unpacking,
> but that would require a change in syntax so it would be much more
> complicated.

January 24th, 2012: http://forum.dlang.org/thread/
mailman.756.1327362275.16222.digitalmars-d at puremagic.com#post-
mailman.757.1327365651.16222.digitalmars-d:40puremagic.com

That said, it is not documented, see this bug: http://d.puremagic.com/
issues/show_bug.cgi?id=7361


More information about the Digitalmars-d mailing list