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

前端 未结 6 1826
北荒
北荒 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:59

    This sounds good in theory, but falls apart in real life in my opinion.

    equals is not based on types and contains is building on top of that.

    That's why code like 1 == BigInt(1) works and returns the result most people would expect.

    In my opinion it doesn't make sense to make contains more strict than equals.

    If contains would be made more strict, code like List[BigInt](1,2,3) contains 1 would stop working completely.

    I don't think “unsafe” or “not type safe” are the right terms here, by the way.

提交回复
热议问题