I've a question regarding the lower type bound.
Given the example of the Scala documentation ( https://docs.scala-lang.org/tour/lower-type-bounds.html ):
Code: Alles auswählen
trait Node[+B] {
def prepend[U >: B](elem: U): Node[U]
}
case class ListNode[+B](h: B, t: Node[B]) extends Node[B] {
def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)
def head: B = h
def tail: Node[B] = t
}
case class Nil[+B]() extends Node[B] {
def prepend[U >: B](elem: U): ListNode[U] = ListNode(elem, this)
}
Code: Alles auswählen
trait Bird
case class AfricanSwallow() extends Bird
case class EuropeanSwallow() extends Bird
Code: Alles auswählen
val africanSwallowList= ListNode[AfricanSwallow](AfricanSwallow(), Nil())
val birdList: Node[Bird] = africanSwallowList
birdList.prepend(new EuropeanSwallow)
Also if I define the generic type of the method call with EuropeanSwallow, sbt shows an error and won't compile:
Code: Alles auswählen
birdList.prepend[EuropeanSwallow](new EuropeanSwallow)
Thanks for the answers and greetings