On Monday, 28 November 2016 at 11:26:41 UTC, dm wrote:
Hi.
Is it possible to write in D something like this?
```
abstract class MyClass(T)
{
public:
@property const(T) value(){return _value;}
@property void value(T val){_value = val;}
...
private:
T _value;
...
}
...
class MyClassFloat: MyClass!float
...
class MyClassInt: MyClass!int
...
void main()
{
MyClass[] someArray;
---^
someArray ~= new MyClassFloat();
...
someArray ~= new MyClassInt();
...
foreach(myClass; someArray)
if(typeid(myClass) == typeid(MyClassInt))
myClass.value = 999;
else
myClass.value = 123.45f;
...
}
```
When I trying to compile code like above I got
Error: class MyClass(T) is used as a type.
Thats because MyClass is a template class. Templates are note
types, instansiations of templates can be types.
e.g.
Myclass!float[] arr; // note this is not MyClass!(float[]);
will work. As Rikki suggested using Object[] instead will allow
use to store classes of different types.