Reputation: 1609
I am learning scala implicits. In below sample code, implicit apply is not getting invoked automatically :
package learn
object ImplicitApplyInClass {
def main(args: Array[String]): Unit = {
implicit val ss = "abc"
//This is working
val a = A(1).apply.toUpperCase
//This is giving compile time error
//val b = A(1).toUpperCase
}
}
case class A(id: Int) {
implicit def apply(implicit s: String) = {
s.toUpperCase
}
}
Kindly suggest why apply is not getting invoked implicitly, when the implicit parameter is available in thr scope?
Upvotes: 0
Views: 2434
Reputation: 170733
When you define an implicit method inside class A
this isn't at all the same as defining an implicit conversion from this class.
It's usable as an implicit in the scope where it's defined, but it's an implicit conversion from String
to String
, which the compiler won't have any reason to use implicitly.
Upvotes: 0
Reputation: 795
You can try this:
object ImplicitApplyInClass {
def main(args: Array[String]): Unit = {
implicit val ss = "abc"
val b = A(1).toUpperCase
println(b)
}
}
object A {
implicit def f(a: A)(implicit s: String): String = s
}
case class A(id: Int)
Upvotes: 0
Reputation: 51658
You can add empty parameter list and this will work:
case class A(id: Int) {
implicit def apply()(implicit s: String) = {
s.toUpperCase
}
}
val b = A(1)().toUpperCase
println(b) // ABC
Here apply
works not as an implicit conversion.
An implicit conversion from type
S
to typeT
is defined by an implicit value which has function typeS => T
, or by an implicit method convertible to a value of that type.Implicit conversions are applied in two situations:
• If an expression
e
is of typeS
, andS
does not conform to the expression’s expected typeT
.• In a selection
e.m
withe
of typeS
, if the selectorm
does not denote a member ofS
.In the first case, a conversion
c
is searched for which is applicable toe
and whose result type conforms toT
. In the second case, a conversionc
is searched for which is applicable toe
and whose result contains a member namedm
.
From here.
In your code it's none of the cases.
Case 1 is not applicable since apply
is String => String
. Case 2 is not applicable since there is no implicit conversion A => { def toUpperCase }
.
Upvotes: 2