Abstract autofunction in D
Is there a way to have a function abstract auto
in D?
If I declare a class like this:
class MyClass
{
abstract auto foo();
}
I am getting the following errors:
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()
I am wondering why this is not possible? And are there any alternatives for getting similar functionality?
source to share
No, because it auto
is a placeholder for a static type. An abstract class cannot know which type should be as it is not specified. Even if it worked, foo()
could return different types based on its derived class implementations. You probably don't want this as it would mean the API might change depending on the implementation.
If you absolutely need this kind of functionality, take a look at std.variant .
source to share