简体   繁体   English

Generic的访问方法,用于扩展特征

[英]Access methods of Generic which extents a trait

for getting a bit deeper into Scala I play around with a mysql wrapper. 为了深入了解Scala,我使用了一个mysql包装器。 No production stuff, just experimenting. 没有生产的东西,只是试验。

I have a trait which specifies an abstract method: 我有一个特征,它指定了一个抽象方法:

trait EntityDefinition {
  def tableName: String
}

Then I have a parameterized class where I want to access tableName: 然后我有一个参数化类,我想访问tableName:

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

Can anyone tell me what I am doing wrong? 谁能告诉我我做错了什么?

You're currently defining EntityDefinition to be a lower bound for T , meaning T should be a supertype of EntityDefinition . 您当前正在将EntityDefinition定义为T下限 ,这意味着T应该是EntityDefinition超类型 What you actually want is for EntityDefinition to be a an upper bound for T , meaning T to be a subtype of EntityDefinition : 你真正想要的是EntityDefinitionT上界 ,意味着TEntityDefinition子类型

class FinagleRepository[T <: EntityDefinition] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM