www.digitalmars.com         C & C++   DMDScript  

D - template constructors?

reply Chris Sauls <ibisbasenji yahoo.com> writes:
Would the following be possible?

/* snip */
template Foo(T) {
     this() {
         // init? registration? factory work? etc
         // called on first use of 'Foo!(bob)' for each different 'bob'
     }
     ~this() {
         // uninit, unregister, retire, etc
         // called alongside module's static ~this()?
         // and/or implement 'delete Foo!(bob);'
     }

     // then the usual junk from here on

     T _bar;

     void baz() {
     }
}
/* /snip */

--Chris S.
--Invironz
Jan 29 2004
parent reply "C" <dont respond.com> writes:
Thats a template function declration , for a class this will work

class Foo(T) {
     this() {
         // init? registration? factory work? etc
         // called on first use of 'Foo!(bob)' for each different 'bob'
     }
     ~this() {
         // uninit, unregister, retire, etc
         // called alongside module's static ~this()?
         // and/or implement 'delete Foo!(bob);'
     }

     // then the usual junk from here on

     T _bar;

     void baz() {
     }
}

void main () {

 Foo!(int) x= new Foo!(int);

}


"Chris Sauls" <ibisbasenji yahoo.com> wrote in message
news:bvbkdr$2js1$1 digitaldaemon.com...
 Would the following be possible?

 /* snip */
 template Foo(T) {
      this() {
          // init? registration? factory work? etc
          // called on first use of 'Foo!(bob)' for each different 'bob'
      }
      ~this() {
          // uninit, unregister, retire, etc
          // called alongside module's static ~this()?
          // and/or implement 'delete Foo!(bob);'
      }

      // then the usual junk from here on

      T _bar;

      void baz() {
      }
 }
 /* /snip */

 --Chris S.
 --Invironz
Jan 29 2004
parent reply Chris Sauls <ibisbasenji yahoo.com> writes:
This is why I ask silly questions.  Knew there had to be a way for it. 
Danke.

Now I assume then that if I had a class defined as:
class Foo(T) {
     this(char[] arg) {
     }
}

That I would instantiate in the obvious way:
void main() {
     Foo!(int) x = new Foo!(int)("arg");
}

And that I could freely use 'Foo!(int)' as a static reference.  If so, 
its all sweetness.

--Chris S.
--Invironz

C wrote:
 Thats a template function declration , for a class this will work
 
 class Foo(T) {
      this() {
          // init? registration? factory work? etc
          // called on first use of 'Foo!(bob)' for each different 'bob'
      }
      ~this() {
          // uninit, unregister, retire, etc
          // called alongside module's static ~this()?
          // and/or implement 'delete Foo!(bob);'
      }
 
      // then the usual junk from here on
 
      T _bar;
 
      void baz() {
      }
 }
 
 void main () {
 
  Foo!(int) x= new Foo!(int);
 
 }
 
 
 "Chris Sauls" <ibisbasenji yahoo.com> wrote in message
 news:bvbkdr$2js1$1 digitaldaemon.com...
 
Would the following be possible?

/* snip */
template Foo(T) {
     this() {
         // init? registration? factory work? etc
         // called on first use of 'Foo!(bob)' for each different 'bob'
     }
     ~this() {
         // uninit, unregister, retire, etc
         // called alongside module's static ~this()?
         // and/or implement 'delete Foo!(bob);'
     }

     // then the usual junk from here on

     T _bar;

     void baz() {
     }
}
/* /snip */

--Chris S.
--Invironz
Jan 29 2004
parent "Robert" <no spam.ne.jp> writes:
Maybe, the following is what you want.

class Foo(T) {
    private static T _bar;

    static this() {
        // initialize _bar
    }
    static ~this() {
        // finalize _bar
    }
    static void baz() {
        // use _bar
    }
}

int main() {
    Foo!(int).baz();
    return 0;
}

"Chris Sauls" <ibisbasenji yahoo.com> wrote in message
news:bvbsld$om$1 digitaldaemon.com...
 This is why I ask silly questions.  Knew there had to be a way for it.
 Danke.

 Now I assume then that if I had a class defined as:
 class Foo(T) {
      this(char[] arg) {
      }
 }

 That I would instantiate in the obvious way:
 void main() {
      Foo!(int) x = new Foo!(int)("arg");
 }

 And that I could freely use 'Foo!(int)' as a static reference.  If so,
 its all sweetness.

 --Chris S.
 --Invironz

 C wrote:
 Thats a template function declration , for a class this will work

 class Foo(T) {
      this() {
          // init? registration? factory work? etc
          // called on first use of 'Foo!(bob)' for each different 'bob'
      }
      ~this() {
          // uninit, unregister, retire, etc
          // called alongside module's static ~this()?
          // and/or implement 'delete Foo!(bob);'
      }

      // then the usual junk from here on

      T _bar;

      void baz() {
      }
 }

 void main () {

  Foo!(int) x= new Foo!(int);

 }


 "Chris Sauls" <ibisbasenji yahoo.com> wrote in message
 news:bvbkdr$2js1$1 digitaldaemon.com...

Would the following be possible?

/* snip */
template Foo(T) {
     this() {
         // init? registration? factory work? etc
         // called on first use of 'Foo!(bob)' for each different 'bob'
     }
     ~this() {
         // uninit, unregister, retire, etc
         // called alongside module's static ~this()?
         // and/or implement 'delete Foo!(bob);'
     }

     // then the usual junk from here on

     T _bar;

     void baz() {
     }
}
/* /snip */

--Chris S.
--Invironz
Jan 29 2004