On Sunday, 10 July 2016 at 21:06:42 UTC, pineapple wrote:
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
    }

The problem you encounter here is that templatized functions cannot be virtual. If you remove "abstract" and put an empty body than it works, but you lose the whole OOP thing, i.e you cannot call the most derived override from the base.

====
import std.conv;

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

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

void main()
{
    SubClass!int obj = new SubClass!int;
    auto x = obj.convertSomePropertyTo!real;
}
===

We are many to encounter this issue. It comes from the fact that the VTBL cannot be build from a template.

Reply via email to