I was looking through some code on GitHub and came across this type declaration:
abstract class TreeNode[BaseType <: TreeNode[BaseType]] extends Product with TreePatternBits {
(This is from the Spark source code, though this question is not really about Spark but Scala's type system.)
This is new to me. In plain English, what does this type bound mean?
Hardly anyone reads white papers on System F (mentioned in the comments) and people are still using F-bound type polymorphism intuitively.
All that you need to understand is that class/trait
MyType[A <: MyType[A]]forces you to extend it asclass Impl extends MyType[Impl], that is passing itself as a type parameter.Which is useful if you want to do something like:
Without this
A <: Interface[A](typeAis a subtype of/extendsImpl[A]) someone could have implemented:It's useful if you have interfaces which e.g. model some algebras e.g.:
To have the implementation the same input/output of methods as its own type:
It's more popular in OOP, as FP offers an alternative approach with the usage of type classes.