【问题标题】:Ensure that parametric type has companion object确保参数类型具有伴随对象
【发布时间】:2017-11-18 23:22:16
【问题描述】:

我想做这样的事情(请不要问为什么):

trait A[T /* tell that there exists companion for T */] {
    def f = T.g
}

有可能实现吗?

【问题讨论】:

    标签: scala companion-object


    【解决方案1】:

    开箱即用是不可能的。您必须使用基于宏的解决方案。前段时间我碰巧发布了a gist这样的解决方案。

    你可以这样使用它:

    def getCompanion[T: HasCompanion] = HasCompanion[T].companion
    

    但是,您不能在 trait 的类型参数上使用上下文边界。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 2019-01-20
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多