On Mon, Jan 23, 2012 at 03:32:07PM -0500, Matt Soucy wrote: > So I was messing around with some code I've been writing recently, > and I wanted to use a foreach on a struct as if it were an > associative array. The problem is, I can't find any information on > how to do that. [...] > I don't see any sort of opApply or similar to do this, and the > foreach section of dlang.org doesn't help. Is there a way to do it, > or do I need to do a workaround? [...]
You can use opApply. Sample test program: struct Test { int opApply(int delegate(ref int) dg) { auto ret = 0; for (auto i=0; ret==0 && i<5; i++) { ret = dg(i); } return ret; } } import std.stdio; void main() { Test t; foreach (n; t) { writeln(n); } } Program prints: 0 1 2 3 4 Hope that helps. T -- If you look at a thing nine hundred and ninety-nine times, you are perfectly safe; if you look at it the thousandth time, you are in frightful danger of seeing it for the first time. -- G. K. Chesterton