Static overloading rules

The definition of static overloading rules is quite long and complicated (it can be found in the official documentation at https://www.scala-lang.org/files/archive/spec/2.13/06-expressions.html#overloading-resolution). It specifies a number of rules that the compiler uses to decide which alternative implicit is chosen. This decision is based on the relative weight of the alternatives. The higher weight means that the alternative A is more specific than B, and A wins. 

The relative weight of A over B is calculated as a sum of two numbers:

  • If A is defined in a class or object which is derived from the class or object defining B (simplified, A is derived from B if A is a subclass or companion object of a subclass of B, or B is a companion object of a superclass of A)
  • If A is as specific as B (simplified, this means that if A is a method, it can be called with the same arguments as B; for polymorphic methods, this also means more specific type constraints)

These two rules allow you to calculate relative weights for two implicit conversions or parameters between 0 and 2 and to select the more suitable alternative in the case of the weights being different. If the weights are equal, the compiler will report ambiguous implicit values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.41.235