有什么方法可以在D中使用abstract auto
功能吗?
如果我按如下方式声明一个类:
class MyClass
{
abstract auto foo();
}
我收到以下错误:
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()
我想知道为什么这不可能?还有其他方法可以获得类似的功能吗?
答案 0 :(得分:6)
不,因为auto
是静态类型的占位符。 abstract 类无法知道应该是什么类型,因为它没有指定。即使这确实有效,foo()
也可能会根据派生类中的实现返回不同的类型。您可能不希望这样,因为这意味着API可能会因实现而异。
如果您绝对需要此类功能,请查看std.variant。