Is it possible to return the subclass from a method of the parent class in dlang?

Steven Schveighoffer schveiguy at yahoo.com
Fri Mar 2 20:39:42 UTC 2018


On 3/2/18 3:23 PM, Christian Köstlin wrote:
> To give an example:
> 
> class Thread {
>    ...
>    Thread start() {...}
> }
> 
> class Timer : Thread {
>    ...
> }
> 
> 
> void main() {
>    // Timer timer = new Timer().start;  // this does not work
>    auto timer = new Timer().start; // because timer is of type Thread
> }

Yes:

class Timer : Thread {
    override Timer start() { ... }
}

https://dlang.org/spec/function.html#virtual-functions

(see item 6)

-Steve


More information about the Digitalmars-d-learn mailing list