www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static typing loop

reply bearophile <bearophileHUGS lycos.com> writes:
Sometimes the static typing gives me problems still. I am creating a recursive
data structure, I'd like to create a class template like Foo1, but with that
the DMD compiler V.1.024 goes into loop eating more and more memory during the
compilation. Foo2 too shares the same problem.
The only way to solve the problem I have found so far is Foo3 (that uses a
std.gc.malloc to allocate memory to be used an array of pointers, that need a
cast every time), but if possible I'd like to use the nicer dynamic arrays as
in Foo1. Do you have some suggestions?

class Foo1(T, int n=10) {
    Foo1!(T[], 20) f;
}

class Foo2(T, int n=10) {
    Foo2!(T*, 20) f;
}

class Foo3(T, int n=10) {
    Foo3!(void*, 20) f;
}

void main() {
    //auto f1 = new Foo1!(int);
    //auto f2 = new Foo2!(int);
    auto f3 = new Foo3!(int);
}

Bye and thank you,
bearophile
Dec 25 2007
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:fkscv5$2js8$1 digitalmars.com...

 class Foo1(T, int n=10) {
    Foo1!(T[], 20) f;
 }
Add a specialization for array types. Or, better yet, specialize using a static if. template isArrayType(T) { const isArrayType = false; } template isArrayType(T : T[]) { const isArrayType = true; } class Foo1(T, int n = 10) { static if(isArrayType!(T)) { // dependent data members declared as T } else { // dependent data members declared as T[] } } Your code looks odd, I'm not entirely sure what you're trying to do; I'm assuming it's that it's a very reduced example.
Dec 25 2007