I’m not sure if this is intended to work, but implicit search fails here
trait Typeclass[Self] {
extension (self: Self) def name: String
}
object Hidden {
given Typeclass[String] with {
extension (self: String) def name: String = self
}
given Typeclass[Int] with {
extension (self: Int) def name: String = self.toString
}
}
class TypeclassBox[T: Typeclass](t: T) {
def apply[U](f: T => Typeclass[T] ?=> U): U = f(t)
}
def branch(b: Boolean): TypeclassBox[_] = {
import Hidden.given
if (b) TypeclassBox("4") else TypeclassBox(3)
}
val box = branch(true)
box(_.name) // error: value name is not a member of Playground.box.T
It works fine if branch
statically returns TypeclassBox[String]
or TypeclassBox[Int]
. I’m not sure if there’s any expectation of implicit search on wildcard types, but it’d be nice!