Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract auto function in D

Tags:

d

Is there any way to have an abstract auto function in D?

If I declare a class as follows:

class MyClass
{
    abstract auto foo();
}

I get 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'm wondering why this isn't possible? And are there any alternatives to obtain similar functionality?

like image 750
Wayzgoose Avatar asked Jul 17 '26 15:07

Wayzgoose


1 Answers

No, because auto is a placeholder for a static type. The abstract class can't know what the type should be as it's not specified. Even if this did work, foo() may return different types based on its implementations in derived classes. You probably don't want that, since it would mean the API could vary depending on the implementation.

If you absolutely need this kind of functionality, look at std.variant.

like image 190
Kiith-Sa Avatar answered Jul 22 '26 06:07

Kiith-Sa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!