Avoiding implicit def ambiguity in Scala

后端 未结 6 1348
悲哀的现实
悲哀的现实 2021-01-07 23:27

I am trying to create an implicit conversion from any type (say, Int) to a String...

An implicit conversion to String means RichString methods (like reverse) are not

6条回答
  •  Happy的楠姐
    2021-01-08 00:07

    As of Scala 2.8, this has been improved. As per this paper (§ Avoiding Ambiguities) :

    Previously, the most specific overloaded method or implicit conversion would be chosen based solely on the method’s argument types. There was an additional clause which said that the most specific method could not be defined in a proper superclass of any of the other alternatives. This scheme has been replaced in Scala 2.8 by the following, more liberal one: When comparing two different applicable alternatives of an overloaded method or of an implicit, each method gets one point for having more specific arguments, and another point for being defined in a proper subclass. An alternative “wins” over another if it gets a greater number of points in these two comparisons. This means in particular that if alternatives have identical argument types, the one which is defined in a subclass wins.

    See that other paper (§6.5) for an example.

提交回复
热议问题