使用特征来实现类型类模式。当我编写原始解决方案时,边缘有点粗糙,因为我假设快速搜索将结构边界转换为上下文边界会得到比我写的更好的解释。情况似乎并非如此。下面是一个编译解决方案。
object Closeables {
trait MyCloseable[A] {
def myClose(a: A): Unit
}
object MyCloseable {
implicit object MyCanBeClosed extends MyCloseable[CanBeClosed] {
def myClose(c: CanBeClosed) = c.nonStandardClose()
}
}
}
class CanBeClosed {
def nonStandardClose(): Unit = println("Closing")
}
import Closeables._
object Test extends App {
def functionThatCloses[A: MyCloseable](a: A) {
implicitly[MyCloseable[A]].myClose(a)
}
def functionThatClosesExplicit[A](a: A)(implicit ev: MyCloseable[A]) {
ev.myClose(a)
}
val c = new CanBeClosed
functionThatCloses(c)
functionThatClosesExplicit(c)
functionThatCloses(c)(MyCloseable.MyCanBeClosed)
functionThatClosesExplicit(c)(MyCloseable.MyCanBeClosed)
}
对于 functionThatClose 可以接受的每种类型的类,您必须在 MyCloseables 中定义和隐式对象。
编译器查看 functionThatCloses 中绑定的上下文,并将其转换为具有 functionThatClosesExplicitly 定义的函数。
编译器从 MyCloseables 对象的定义中“找到”隐含的“证据”并使用它。