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.