I'm learning implicit methods in Scala. For instance consider the following test code:
object Test{
def main(args: Array[String]) = {
implicit val f: Int => String = (_: Int).toString + "sdgfdfsg"
val Ext(i) = 10
println(i)
}
}
object Ext{
def unapply(i: Int)(implicit f: Int => String): Option[String] = Some(f(i))
}
This code works as expected. But the thing that is not clear to me is why it works. So I read about Scala compiler phases and figured out that all desugaring is performed during the first phase:
parser 1 parse source into ASTs, perform simple desugaring
But what is the order of desugaring? It seems in the example the
val Ext(i) = 10
was first desugared to
val i = Ext.unapply(10)
and then compiler found the implicit value implicit val f: Int => String = (_: Int).toString + "sdgfdfsg" and the final desugared version is
val i = Ext.unapply(10)(f)
Why are implicits desugared later? Or how is it working?