Map one tuple to another Tuple of different type

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 22 09:40:35 PDT 2014


On Tue, Jul 22, 2014 at 03:52:14PM +0000, Vlad Levenfeld via Digitalmars-d-learn wrote:
> I'm just confused about how static while is supposed to work because
> static foreach, to my understanding, would have to work by making a
> new type for each iteration. I say this because, 1) runtime foreach
> works like that (with type => range), and 2) without ctfe foreach, the
> only way I know of to iterate a typelist is to make a new type with
> one less element, so I imagine static foreach lowers to that.
> 
> I suppose its possible to make a struct with static immutable start
> and end iterators, and make new types out of advancing the start
> iterator until it was equal to the end. Seems like a step backward
> though.
> 
> Anyway my actual question is: if all values are constant at compile
> time, how would a static while loop terminate?

Basically, think of it as custom loop unrolling:

	TypeTuple!(
		int, "x",
		float, "y",
		uint, "z"
	) t;

	// This loop:
	foreach (i; staticIota(0, 3)) {
		t[i]++;
	}

	// Is equivalent to:
	t[0]++;
	t[1]++;
	t[2]++;

	// Which is equivalent to:
	t.x++;
	t.y++;
	t.z++;

The loop body is basically expanded for each iteration, with the loop
variable suitably substituted with each element of the typelist.


T

-- 
Microsoft is to operating systems & security ... what McDonalds is to gourmet cooking.


More information about the Digitalmars-d-learn mailing list