Inheriting a trait twice

前端 未结 1 1528
情话喂你
情话喂你 2020-12-18 19:29

This doesn\'t work:

trait Trait
class Class extends Trait with Trait

Compiler complains:

:8: error: trait Tr         


        
相关标签:
1条回答
  • 2020-12-18 20:19

    Second snippet works because of trait linearization. The compiler will organize the traits into a linear list so that Trait only appears once. I think the linearization is

    Implementation, Trait, Abstraction, ScalaObject, AnyRef, Any
    

    See this chapter from Programming Scala for a great explanation.

    This is primarily done to have a consistent approach to the diamond inheritance problem and is useful in that case.

    Since Trait cannot appear twice after linearization, it does not make sense to write Trait with Trait and it makes sense to be disallowed.

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