Use class template as a type
rikki cattermole via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 28 06:01:32 PST 2016
On 29/11/2016 2:56 AM, dm wrote:
> On Monday, 28 November 2016 at 11:30:23 UTC, rikki cattermole wrote:
>> In your case I'd just swap out ``MyClass[] someArray;`` to ``Object[]
>> someArray;``.
>> But only because there are no members added without the extra typing
>> in MyClass.
>>
>> Remember types in meta-programming in D are not erased, they exist in
>> the assembly and are unique. Unlike Java who did the implementation
>> rather wrong.
>
> I'm tried to use Object[], but got error
> Error: no property 'value' for type 'object.Object'
> I guess I must cast() to MyClassInt or MyClassFloat, but how can I do it?
We have a handy dandy syntax for this:
if (MyClassInt subclass = cast(MyClassInt)value) {
writeln(subclass.value);
}
If it doesn't cast to said type (it will be null) that branch won't execute.
More information about the Digitalmars-d-learn
mailing list