Selfreferencing Generics

scala

Solution

The structure itself is explained in the Twitter Scala School and is called F-bounded polymorphism.

// you define it like this
trait X extends Person[X]

// it then gets expanded to this
trait Person[X extends Person[X]]

This structure is used when the trait needs to have a reference to the type of the object it is extending. If the Scala school explanation is not enough you can search the internet for 'F-bounded polymorphism'

Problem

I've searched a bit around but I was not able to find an example of such a structure: ``` Person[P <: Person[P]] ``` that is explained in a way I understand. How does this resolve? It kinda looks like an endless recursion to me, but it seems I am wrong with that conclusion.

Original source