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

Jacob Carlborg doob at me.com
Tue Mar 6 17:53:11 UTC 2018


On 2018-03-02 21:23, 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
> }

You can also try a template this parameter [1] in the base class:

class Thread
{
     T start(this T) () { ... }
}

But if this "Thread" is core.thread.Thread that won't work.

[1] https://dlang.org/spec/template.html#template_this_parameter

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list