How to distinguish between objects with different abstract type fields?

前端 未结 3 1470
天命终不由人
天命终不由人 2021-01-15 21:58

Assume, we have an abstract class with an abstract type field:

abstract class A {type T}

Now let\'s assume, we have a method, which returns

3条回答
  •  情书的邮戳
    2021-01-15 22:15

    You can use the <%< which tells you in compile-time if two types are compatible.

    scala> class A { type T }
    defined class A
    
    scala> implicitly[A { type T=String } <%< A { type T=Int } ]
    :9: error: could not find implicit value for parameter e: <%<[A{type T = String},A{type T = Int}]
                  implicitly[A { type T=String } <%< A { type T=Int } ]
                            ^
    
    scala> implicitly[A { type T=String } <%< A { type T=String } ]
    res1: <%<[A{type T = String},A{type T = String}] = 
    
    scala> implicitly[A { type T=String } <%< A ]
    res3: <%<[A{type T = String},A] = 
    

提交回复
热议问题