D中的抽象自动函数

Way*_*ose 3 d

有没有办法abstract auto在D中有功能?

如果我按如下方式声明一个类:

class MyClass
{
    abstract auto foo();
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

main.d(12): Error: function declaration without return type. (Note that constructors are always named 'this')
main.d(12): Error: no identifier for declarator foo()
Run Code Online (Sandbox Code Playgroud)

我想知道为什么这是不可能的?有没有其他方法可以获得类似的功能?

小智 6

不,因为auto静态类型的占位符.该抽象类不能知道类型应该是什么不指定它.即使这确实有效,foo()也可以根据派生类中的实现返回不同的类型.您可能不希望这样,因为这意味着API可能会因实现而异.

如果您绝对需要这种功能,请查看std.variant.