【发布时间】:2017-11-18 23:22:16
【问题描述】:
我想做这样的事情(请不要问为什么):
trait A[T /* tell that there exists companion for T */] {
def f = T.g
}
有可能实现吗?
【问题讨论】:
我想做这样的事情(请不要问为什么):
trait A[T /* tell that there exists companion for T */] {
def f = T.g
}
有可能实现吗?
【问题讨论】:
开箱即用是不可能的。您必须使用基于宏的解决方案。前段时间我碰巧发布了a gist这样的解决方案。
你可以这样使用它:
def getCompanion[T: HasCompanion] = HasCompanion[T].companion
但是,您不能在 trait 的类型参数上使用上下文边界。
【讨论】: