确保参数类型具有伴随对象
Ensure that parametric type has companion object
我想做这样的事情(请不要问为什么):
trait A[T /* tell that there exists companion for T */] {
def f = T.g
}
有可能实现吗?
开箱即用是不可能的。您必须使用基于宏的解决方案。我刚好在前一段时间发布了这样的解决方案a gist。
你可以这样使用它:
def getCompanion[T: HasCompanion] = HasCompanion[T].companion
但是,您不能在特征的类型参数上使用上下文边界。
我想做这样的事情(请不要问为什么):
trait A[T /* tell that there exists companion for T */] {
def f = T.g
}
有可能实现吗?
开箱即用是不可能的。您必须使用基于宏的解决方案。我刚好在前一段时间发布了这样的解决方案a gist。
你可以这样使用它:
def getCompanion[T: HasCompanion] = HasCompanion[T].companion
但是,您不能在特征的类型参数上使用上下文边界。