module test;
import std.stdio;

class buffer(T, size_t sz) {
        auto arr = new T[sz];
        enum end = sz-1;
}

void foo(T, size_t sz)() {
        auto buf = new buffer!(T,sz);
        writeln("before ", buf.arr);
        foreach(ref ele; buf.arr) ++ele;
        writeln("after ", buf.arr);
}

unittest {
        foo!(uint,4);
        foo!(uint,4);

        auto a = new buffer!(uint,4);
        writeln(a.arr);
        auto b = new buffer!(uint,4);
        writeln(b.arr);
}

void main() {
        auto c = new buffer!(uint,4);
        writeln(c.arr);
}

rdmd -unittest test.d
before [0, 0, 0, 0]
after [1, 1, 1, 1]
before [1, 1, 1, 1]
after [2, 2, 2, 2]
a [2, 2, 2, 2]
b [2, 2, 2, 2]
c [2, 2, 2, 2]
[Finished in 1.3s]

I narrowed this down after much frustration. I'm using some fixed size buffers and thought it useful to define the size as part of the type, from which other aspects could be statically derived. Apparently it's not so useful. So I guess I can refactor the buffer's array length into the constructor or something, but I really didn't see this kind of memoization(?) coming.

Reply via email to