Jay Taylor's notes
back to listing indexscala - define method to return type of class extending it - Stack Overflow
[web search]
I'd like to be able to do something like this:
And the following would not compile, given the above code
Is there a name for this kind of relationship? And how is it annotated/implemented in Scala? EditThe following sort of works, as you can see:
Is there a way to get around that? Edit 2Using the second system, I can do this, which is good up to the definition of class D:
| ||||
add comment | ||||
I'm afraid there is no possibility to know what is the extended class from the extending class. The closest to what you'd like to have is something similar to Curiously Recurring Template Pattern (CRTP) well known from C++.
| |||||||||||||||
|
One thing that you can do, is to return type
This can be useful for builders. | |||||||||||||||
|
Here is another possible solution. It's combination of self type + type parameter:
Here you can find more info about this solution: | |||||||||
|