Concrete classes with abstract type members

后端 未结 2 1426
一个人的身影
一个人的身影 2020-12-06 07:44

Given the following traits and class. Why does this compile? Can this be actually used for something?

trait Container {
  type A
}

trait AnotherContainer[B         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 08:12

    In your example, the compiler adds the default type bounds of >: Nothing <: Any. The second example below shows a case where an abstract type becomes usable (if not useful).

    scala> trait T { type A >: Nothing <: Any }
    defined trait T
    
    scala> 1: T#A
    :6: error: type mismatch;
     found   : Int(1)
     required: T#A
           1: T#A
           ^
    
    scala> trait T { type A >: Int <: Int }
    defined trait T
    
    scala> 1: T#A                          
    res6: T#A = 1
    
    scala> "": T#A
    :6: error: type mismatch;
     found   : java.lang.String("")
     required: T#A
           "": T#A
           ^
    

提交回复
热议问题