www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - define new types within templates

reply teo <teo.ubuntu.remove yahoo.com> writes:
There was a way to define new types within templates and I think that I 
have seen that demonstrated here in the newsgroups, but cannot find it 
now. Can someone help me please?

I would like to do something like this:

template MyTemplate(T)
{
  struct T ~ "Struct"  // define FooStruct structure
  {
    int x;
  }
  class T ~ "Class"  // define FooClass class
  {
    void bar(T ~ "Struct" t)
    {
      // ...
    }
  }
}

void main()
{
  mixin MyTemplate!("Foo");
  FooStruct t;
  FooClass f = new FooClass();
  f.bar(t);
}

Hopefully I am not mistaken.
Dec 29 2009
next sibling parent "Phil Deets" <pjdeets2 gmail.com> writes:
On Tue, 29 Dec 2009 17:44:18 -0500, teo <teo.ubuntu.remove yahoo.com>  
wrote:

 There was a way to define new types within templates and I think that I
 have seen that demonstrated here in the newsgroups, but cannot find it
 now. Can someone help me please?

 I would like to do something like this:

 template MyTemplate(T)
 {
   struct T ~ "Struct"  // define FooStruct structure
   {
     int x;
   }
   class T ~ "Class"  // define FooClass class
   {
     void bar(T ~ "Struct" t)
     {
       // ...
     }
   }
 }

 void main()
 {
   mixin MyTemplate!("Foo");
   FooStruct t;
   FooClass f = new FooClass();
   f.bar(t);
 }

 Hopefully I am not mistaken.
Did you try string mixins? Maybe something like this untested code: template MyTemplate(T) { mixin (CtfeReplace(q{ struct _T_Struct { int x; } }, "_T_", T.stringof)); mixin (CtfeReplace(q{ class _T_Class { void bar(_T_Struct t) { // ... } } }, "_T_", T.stringof)); } Sorry if this is a duplicate post. I attempted to cancel my earlier post.
Dec 29 2009
prev sibling next sibling parent grauzone <none example.net> writes:
teo wrote:
 There was a way to define new types within templates and I think that I 
 have seen that demonstrated here in the newsgroups, but cannot find it 
 now. Can someone help me please?
 
 I would like to do something like this:
 
 template MyTemplate(T)
 {
   struct T ~ "Struct"  // define FooStruct structure
   {
     int x;
   }
   class T ~ "Class"  // define FooClass class
   {
     void bar(T ~ "Struct" t)
     {
       // ...
     }
   }
 }
 
 void main()
 {
   mixin MyTemplate!("Foo");
   FooStruct t;
   FooClass f = new FooClass();
   f.bar(t);
 }
 
 Hopefully I am not mistaken.
I don't know for what you're asking, but there's absolutely no need to use string mixins (see Trass3r's reply). Instead you can just define a type inside the template, and then access it by instantiating it: template MyTemplate(T) { struct Struct { T x; } } void main() { MyTemplate!(int).Struct t; t.x = 123; } You can use mixin MyTemplate!(int); to bring "Struct" into the module namespace, and you can use e.g. "alias MyTemplate!(int) IntStruct;" to save typing.
Dec 29 2009
prev sibling parent BCS <none anon.com> writes:
Hello teo,

 There was a way to define new types within templates and I think that
 I have seen that demonstrated here in the newsgroups, but cannot find
 it now. Can someone help me please?
 
 I would like to do something like this:
 
 template MyTemplate(T)
 {
 struct T ~ "Struct"  // define FooStruct structure
 {
 int x;
 }
 class T ~ "Class"  // define FooClass class
 {
 void bar(T ~ "Struct" t)
 {
 // ...
 }
 }
 }
 void main()
 {
 mixin MyTemplate!("Foo");
 FooStruct t;
 FooClass f = new FooClass();
 f.bar(t);
 }
 Hopefully I am not mistaken.
 
what I would do is this: template MyTemplate(char[] name) { struct S { int x; } class C { ... } // put the minimum you can in the string mixin. mixin ("alias S "~name~"Struc; alias C "~name~"Class;"); }
Dec 29 2009