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
e
is of typeT
, andT
does not conform to the expression’s expected typept
. In this case, an implicitv
which is applicable toe
and whose result type conforms topt
is 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 expressione
is 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.m
withe
of typeT
, if the selectorm
does not denote an accessible member ofT
. In this case, a viewv
which is applicable toe
and whose result contains an accessible member namedm
is 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.m
is converted tov(e).m
.- In an application
e.m(args)
withe
of typeT
, if the selectorm
denotes some accessible member(s) ofT
, but none of these members is applicable to the argumentsargs
. In this case, a viewv
which is applicable toe
and whose result contains a methodm
which is applicable toargs
is 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…