Methods are polymorphic; variables are not. Therefore, instead of making the voice variable, you want to override speak in the child.
In addition, the return type auto does not work with polymorphism, you need to specify types. (The reason is that automatic return makes a function template in the compiler, which theoretically can have several redefinable slots in the function table, so it just doesn't try to insert it.)
So try this:
import std.stdio; class Animal { void speak() {
If you have a lot of common functionality and want to reuse a single function with minor changes to child classes, you can make a method instead of a variable that just returns something.
Like string voice() { return "woof"; } string voice() { return "woof"; } , it can be overridden in child elements.
source share