4

Hi I come across this piece of code, but could not understand this. The confusing place is "V <: Vector[V]", this means V is a subtype of Vector[V] ? very confusing here.

trait Vector[V <: Vector[V]] { this: V =>
  def +(other: V): V
}
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
zjffdu
  • 25,496
  • 45
  • 109
  • 159

1 Answers1

6

It's called F-bounded type polymorphism and

is usually attempted when someone is trying to solve a common problem of abstraction in object-oriented languages: how to define a polymorphic function that, though defined in terms of a supertype, will when passed a value of some subtype will always return a value of the same subtype as its argument.

(from a recent blog post "F-Bounded Type Polymorphism Considered Tricky" by Kris Nuttycombe)

Also see this SO question: What does "Recursive type bound" in Generics mean?

Community
  • 1
  • 1
sourcedelica
  • 23,940
  • 7
  • 66
  • 74