Why I can't create F-bounded object in Scala

前端 未结 1 1562
北荒
北荒 2021-01-13 03:29

Suppose I have:

trait A[AA <: A[AA]]
//or even just `
trait A[AA]

This doesn\'t work:

scala> object AAA extends A[AAA         


        
相关标签:
1条回答
  • 2021-01-13 03:52

    As you allude to in your title, the working case class AAA extends A[AAA] is an example of F-bounded polymorphism, which is a recursive type definition where the definition refers to itself. Recursion is fairly common in types, even the humble List is recursive; it's fairly well understood territory.

    However, object AAA extends A[AAA.type] is not a recursive type. Here AAA is a value, and your declaration asks the compiler to resolve the reference to a value's type while it is being defined, which is not a capability Scala was designed/intended to have.

    0 讨论(0)
提交回复
热议问题