www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: SListRange, Ranges, costructors

 1) I've seen that the Node struct inside std.range.SListRange is not a
 static struct, is it a small bug?

Bug 4087. Currently it's not a bug.
 2) In that List(T) I've used the Range protocol. But I've had to
 add a reset() function too. Isn't it useful for foreach() to call
 a function similar to reset() (if present) at the begin of the
 iteration, to set the variables necessary for the iteration?

I've taken a look at SListRange, and it seems the p pointer is useless, this works, and there is no need for the reset(): import std.stdio; struct List(T) { static struct Node { T data; Node* next; this(T d, Node* p) { data = d; next = p; } } Node* lh; this(T[] arr) { foreach_reverse (el; arr) lh = new Node(el, lh); } bool empty() { return !lh; } T front() { return lh.data; } void popFront() { lh = lh.next; } } void main() { List!int items = [1, 2, 3]; foreach (x; items) write(x, " "); // prints 1 2 3 writeln(); assert(items.lh); foreach (x; items) write(x, " "); // prints 1 2 3 } But I feel dumb and I don't understand, why isn't lh null at the end of the first foreach?
 3) The stack initalization of a struct has some built-in sugar,
 while to allocate the struct on the stack you need a this() method:

Enhancement request 4086. Bye, bearophile
Apr 13 2010