How to find implicit function(s) using Scala metaprogramming?

147 views Asked by At

In scala language, the implicit scope of a class is erased in runtime, e.g. if defining:

case class A(i: Int)

object A {

  implicit def toInt(a: A) = a.i
}

Then for an array:

val arr = Array[Any](A(1), 2)

it is impossible to write:

arr.map(_ + 3)

as type A and its implicit scope is removed after being inserted to Array[Any].

This makes some design patterns (e.g. type class lookup) impossible to be applied in runtime (when type information is still partially available). In my case, I would like to write the above program without explicitly stating A in type matching (as in real case the number of classes like A can be countless). Is any scala metaprogramming package (scalameta preferred) capable of doing this in one project? What should I do to tell the Java runtime bytecode to "look for implicit for a runtime class, if not found do something else"?

0

There are 0 answers