www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Instantiating a class with different types at runtime

reply Marduk <mardukbp mac.com> writes:
Dear all,

I would like to have a kind of template class like the following:

   class Example {

     this(Type_left x, Type_right y) {
       this.left = x;
       this.right = y;
     }

     Type_left left;
     Type_right right;

   }

Such that at runtime I can instantiate it with different types:

new Example(int a, int b);

new Example(int a, string b);

I have read about templates and abstract classes, but I have not 
figured how to get this to work. Thanks.
Nov 27 2016
next sibling parent reply Namespace <rswhite4 gmail.com> writes:
On Sunday, 27 November 2016 at 20:52:06 UTC, Marduk wrote:
 Dear all,

 I would like to have a kind of template class like the 
 following:

   class Example {

     this(Type_left x, Type_right y) {
       this.left = x;
       this.right = y;
     }

     Type_left left;
     Type_right right;

   }

 Such that at runtime I can instantiate it with different types:

 new Example(int a, int b);

 new Example(int a, string b);

 I have read about templates and abstract classes, but I have 
 not figured how to get this to work. Thanks.
class Example(L, R) { L _left; R _right; this(L l, R r) { _left = l; _right = r; } }
Nov 27 2016
parent Marduk <mardukbp mac.com> writes:
On Sunday, 27 November 2016 at 20:57:28 UTC, Namespace wrote:
 class Example(L, R)
 {
     L _left;
     R _right;

     this(L l, R r)
     {
         _left = l;
         _right = r;
     }
 }
That was fast! But I needed the second reply in order to understand yours. Thanks anyway.
Nov 28 2016
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 11/27/2016 09:52 PM, Marduk wrote:
   class Example {

     this(Type_left x, Type_right y) {
       this.left = x;
       this.right = y;
     }

     Type_left left;
     Type_right right;

   }

 Such that at runtime I can instantiate it with different types:

 new Example(int a, int b);

 new Example(int a, string b);
Turn Example into a template, and add a free function for nice construction: ---- class Example(Type_left, Type_right) { /* ... as you had it ... */ } Example!(L, R) makeExample(L, R)(L x, R y) { return new Example!(L, R)(x, y); } void main() { auto foo = makeExample(1, 2); auto bar = makeExample(3, "baz"); } ---- Note that Example is not a type, but a template. That means, foo and bar have different types, because their types are different instantiations of the Example template. You can define a common interface or (possibly abstract) base class.
Nov 27 2016
parent Marduk <mardukbp mac.com> writes:
On Sunday, 27 November 2016 at 21:06:58 UTC, ag0aep6g wrote:
 Turn Example into a template, and add a free function for nice 
 construction:

 ----
 class Example(Type_left, Type_right)
 {
     /* ... as you had it ... */
 }

 Example!(L, R) makeExample(L, R)(L x, R y)
 {
     return new Example!(L, R)(x, y);
 }

 void main()
 {
     auto foo = makeExample(1, 2);
     auto bar = makeExample(3, "baz");
 }
 ----

 Note that Example is not a type, but a template. That means, 
 foo and bar have different types, because their types are 
 different instantiations of the Example template. You can 
 define a common interface or (possibly abstract) base class.
Great! Many thanks.
Nov 28 2016