Concrete classes with abstract type members

后端 未结 2 1409
一个人的身影
一个人的身影 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:09

    It looks harmless if useless to me. The type that x wants doesn't exist, so you can't pass it to the method. Whether harmless uselessness should be a compile-time error is a matter of taste, I suppose.

    If you look at what x actually does, it decompiles thusly:

    public java.lang.Object x(java.lang.Object);
      Code:
       0:   aload_1
       1:   areturn
    

    which is exactly what the identity method should do (load the argument regardless of type, return it). You can write something equivalent with much less code:

    trait AbstractType { type T }
    class Useless extends AbstractType { def identity(t: AbstractType#T) = t }
    

    Except nothing has type AbstractType#T, so again we have uselessness.

    Unless I'm missing something.

    0 讨论(0)
  • 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
    <console>: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
    <console>:6: error: type mismatch;
     found   : java.lang.String("")
     required: T#A
           "": T#A
           ^
    
    0 讨论(0)
提交回复
热议问题