Defining and overriding methods of an abstract base class which must accept a template parameter

pineapple via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jul 10 14:06:42 PDT 2016


This is essentially what I'm trying to accomplish. The intuitive 
solution, of course, does not work. In theory I could write a 
separate method for every anticipated return type, but that would 
be horrible and in that case I'd probably just write the damn 
thing in a dynamically-typed language instead.

     import std.conv;

     abstract class BaseClass{
         abstract X convertSomePropertyTo(X)();
     }

     class SubClass(T): BaseClass{
         T property;
         X convertSomePropertyTo(X)(){
             return property.to!X;
         }
     }

     void main(){
         BaseClass obj = new SubClass!int;
         auto x = obj.convertSomePropertyTo!real; // Error: 
function 
test.BaseClass.convertSomePropertyTo!real.convertSomePropertyTo 
non-virtual functions cannot be abstract
     }



More information about the Digitalmars-d-learn mailing list