www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is it possible to return the subclass from a method of the parent

reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
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
}


thanks in advance,
christian
Mar 02 2018
next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Mar 02 2018
parent reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
On 02.03.18 21:39, Steven Schveighoffer wrote:
 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
Thanks for this. It works for me only without the override (with override I get Error: function timer.Timer.start does not override any function, did you mean to override 'core.thread.Thread.start'?). Although I wonder if its possible to "fix" this in the Thread class with some dlang magic. e.g. traits class Thread { traits(GetClass) start() {...} } or perhaps class ThreadHelper(T) : Thread { override T start() {return cast(T)super.start();} } class Timer : Thread!Timer { }
Mar 02 2018
parent reply =?UTF-8?Q?Christian_K=c3=b6stlin?= <christian.koestlin gmail.com> writes:
 class Timer : Thread {
    override Timer start() { ... }
 }

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

 (see item 6)

 -Steve
Thanks for this. It works for me only without the override (with override I get Error: function timer.Timer.start does not override any function, did you mean to override 'core.thread.Thread.start'?).
This seems to be connected to Thread.start being a final function.
Mar 02 2018
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Mar 03, 2018 at 01:13:43AM +0100, Christian Kstlin via
Digitalmars-d-learn wrote:
 class Timer : Thread {
  override Timer start() { ... }
 }

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

 (see item 6)

 -Steve
Thanks for this. It works for me only without the override (with override I get Error: function timer.Timer.start does not override any function, did you mean to override 'core.thread.Thread.start'?).
This seems to be connected to Thread.start being a final function.
Well, yes, you cannot override a final function. That is the point of `final`. :-D If you meant to override it after all, remove the `final`. T -- Life is complex. It consists of real and imaginary parts. -- YHL
Mar 02 2018
prev sibling parent Jacob Carlborg <doob me.com> writes:
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
Mar 06 2018