What does Node [TypeOne <: Node [TypeOne]] mean in scala?

I can’t understand how this code works?

class Node[TypeOne <: Node[TypeOne]] {
  var x: TypeOne = _
}


object Tree extends App {
  val treeNode = new Node[String]
  treeNode.x = "ten"
  //treeNode.x = new TreeNode[String]
}

I originally thought of the Node signature class [TypeOne <: Node [TypeOne]], it meant that any type x variable of type TypeOne should be of type Node or its subclass, but it doesn't seem like that, since I can create a val treeNode of type Node [ String] and Node [Int]. So what does this signature do?

+4
source share
2 answers

This pattern is usually used for base classes or features that want to know their specific subtype statically. Most likely, the library designer expects you to continue Node:

class MyNode extends Node[MyNode]

MyNode Node.

+3

Source: https://habr.com/ru/post/1653766/


All Articles