【问题标题】:Access methods of Generic which extents a trait扩展特征的通用访问方法
【发布时间】:2016-05-27 14:48:20
【问题描述】:

为了更深入地了解 Scala,我使用了 mysql 包装器。没有生产的东西,只是试验。

我有一个指定抽象方法的特征:

trait EntityDefinition {
  def tableName: String
}

然后我有一个参数化类,我想在其中访问 tableName:

class FinagleRepository[T >: EntityDefinition] @Inject()(client: FinagleMysqlClient) {
  def create(entity: T): Future[Result] = {
    // Here it is impossible to call T.tableName
  }
}

谁能告诉我我做错了什么?

【问题讨论】:

    标签: scala generics traits


    【解决方案1】:

    您目前将EntityDefinition 定义为Tlower bound,这意味着T 应该是EntityDefinition超类型。你真正想要的是EntityDefinition 成为Tupper bound,这意味着T 成为EntityDefinition子类型

    class FinagleRepository[T <: EntityDefinition] 
    

    【讨论】:

    • 您好 Yuval,感谢您的回答。如此愚蠢的错误...... :-)
    猜你喜欢
    • 2016-08-11
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 2021-01-10
    • 2015-06-03
    相关资源
    最近更新 更多