How does Scala 3 implement implicit conversions with implicit parameters?

In Scala2, I sometimes write implicit conversions with implicit parameters like this:

implicit def convert(i: Int)(implicit ev: Runnable[Int]): Unit = 
    ev.r(i) // method r is a method inside ev

However, I don’t know how to write this in Scala3 with given conversion. The Conversion needs to take exactly 2 type parameters but I need more:

given Conversion[???, Unit] with
    def apply(i: Int)(using ev: Runnable[Int]): Unit = 
        i.r() // method r is an extension method inside ev

What should I code inside Conversion[???, Unit] ?

Something like this should work:

given intUnitConv(using ev: Runnable[Int]): Conversion[Int, Unit] with
  def apply(i: Int): Unit = ev.r(i)

I’d be very suspicious of hiding side-effecting computations behind an implicit conversion this way, though. I’d also be curious how you ensure that the Scala builtin value discarding is not triggered with precedence over your custom mechanism…?

1 Like