Hello all,
Following recent discussions about restricting implicit conversions, I was thinking about perhaps one of the most basic use of these regarding numeric promotion, for instance from Int to BigInt as in:
val a = BigInt(1)
val b = a + 1
This is enabled by the definition below in object BigInt, together with this spec excerpt:
implicit def int2bigInt(i: Int): BigInt = apply(i)
If an expression
eis of typeT, andTdoes not conform to the expression’s expected typept. In this case, an implicitvwhich is applicable toeand whose result type conforms toptis searched. The search proceeds as in the case of implicit parameters, where the implicit scope is the one ofT => pt. If such a view is found, the expressioneis converted tov(e)
Implicit Conversions - More Details (scala-lang.org)
Here the type of the parameter to + is Int whereas its expected type is BigInt, so an implicit conversion is looked up in the implicit scope of Int => BigInt, which includes BigInt’s companion object. So far so good. Now, I am looking for where in the specification is the following allowed:
val c = 1 + a
In the above reference we have:
- In a selection
e.mwitheof typeT, if the selectormdoes not denote an accessible member ofT. In this case, a viewvwhich is applicable toeand whose result contains an accessible member namedmis searched. The search proceeds as in the case of implicit parameters, where the implicit scope is the one ofT. If such a view is found, the selectione.mis converted tov(e).m.- In an application
e.m(args)witheof typeT, if the selectormdenotes some accessible member(s) ofT, but none of these members is applicable to the argumentsargs. In this case, a viewvwhich is applicable toeand whose result contains a methodmwhich is applicable toargsis searched. The search proceeds as in the case of implicit parameters, where the implicit scope is the one ofT. If such a view is found, the applicatione.m(args)is converted tov(e).m(args)
, but I cannot quite make the link with the conversion method above in terms of implicit scope, if someone has an idea…