Generalized structural type conformance in Scala

后端 未结 1 1519
无人共我
无人共我 2021-01-03 01:47

I\'m interested in the problem of conforming a specific type to a more general structural type. Consider the following examples:

trait Sup

trait Sub extend         


        
相关标签:
1条回答
  • 2021-01-03 02:12

    One concern is that when you read this code:

    self: { def copy(version: Int): T }
    

    you do not expect the name of the parameter to be significant, as it would have to be in this example:

    case class Robot(number: Int, override val version: Int)
      extends Versionable[Robot]
    

    EDIT: On another note, regarding the lack of parameter contravariance for methods, you can do:

    type General = { val contra: (Sub => Unit) }
    class B { val contra = ((o:Sup) => println(o)) }
    var b:General = new B
    
    0 讨论(0)
提交回复
热议问题