Short answer: no. Scala's compiler will only ever look to apply a single implicit, so if it fails to spot an implicit int
lying around, it will stop and give up.
However, you could write your purchaseCandles
method to operate on types that can be converted to an Int
, and require a parameter of that type:
def purchaseCandles[A <% Int]()(implicit age : A) = {
val asAge : Int = age
println(s"I'm going to buy $asAge candles!")
}
The asAge
part is necessary to force the application of the implicit conversion.
As of yet, I seem to need to specify the type of A
in this scenario, though I can't work out why: since there shouldn't be other values around of types that can be implicitly converted to Int
(this happens with brand new types as well, so it's not the ubiquity of Int
.) But you can do:
{
implicit val guest = Monkey(10000)
purchaseCandles[Monkey]()
}
This use of implicits, however, is probably a bad idea!