Selectively disable subsumption in Scala? (correctly type List.contains)

前端 未结 6 1815
北荒
北荒 2020-12-16 11:04
List(\"a\").contains(5)

Because an Int can never be contained in a list of String

6条回答
  •  青春惊慌失措
    2020-12-16 11:45

    I think you misunderstand Martin's solution, it is not B <: Eq, it is B : Eq, which is a shortcut for

    def Contains[B >: A](x: B)(implicit ev: Eq[B])
    

    And Eq[X] would then contains a method

    def areEqual(a: X, b: X): Boolean
    

    This is not the same as moving the equals method of Any a little lower in the hierarchy, which would indeed solve none of the problem of having it in Any.

提交回复
热议问题