简体   繁体   English

在Scala中使用泛型实现特征的正确方法是什么?

[英]What is the correct way to implement trait with generics in Scala?

I have some simple traits (Entity in the example below) that are extended by case classes in my app. 我有一些简单的特征(在下面的示例中为Entity),这些特征由我的应用中的案例类扩展。 I would like to create an EntityMapper trait that provides an interface for handling the case classes that extend the Entity trait (Foo in the example below). 我想创建一个EntityMapper特性,该特性提供一个接口来处理扩展了Entity特性的案例类(在下面的示例中为Foo)。 I thought I should be able to do this fairly easily using generics and bounding but I've spent a couple of hours on it already and I haven't gotten it to work correctly. 我以为我应该可以使用泛型和边界相当容易地做到这一点,但是我已经花了几个小时在上面,但我还没有使其正常工作。 The code below is what I think I should be able to do but it fails with a compiler error. 下面的代码是我认为应该能够执行的操作,但是由于编译器错误而失败。 The error is 错误是

Test.scala:15: error: value id is not a member of type parameter Foo \\ println(e.id) Test.scala:15:错误:值id不是类型参数Foo \\ println(e.id)的成员

package experiment

trait Entity {
    val id: Option[Long]
}

case class Foo(val id: Option[Long] = None) extends Entity

trait EntityMapper {
    def create[E <: Entity](e: E): E
}

object FooMapper extends EntityMapper {
    def create[Foo](e: Foo): Foo = {
        println(e.id)
        e
    }
}

object Main extends App {
    val foo = FooMapper.create(Foo(None))
}

I've tried several different things to solve the problem but nothing has worked. 我已经尝试了几种不同的方法来解决问题,但是没有任何效果。 If I comment out the line in question "println(e.id)", it compiles but that is not useful because I cannot access or modify any of the properties of Foo. 如果我注释掉问题“ println(e.id)”中的行,它会编译,但没有用,因为我无法访问或修改Foo的任何属性。

I have tried using a covariant argument to the mapper trait and then supplying the type to the FooMapper object definition but that yields the same error. 我尝试对映射器特征使用协变参数,然后将类型提供给FooMapper对象定义,但这会产生相同的错误。 The code for that attempt is below: 该尝试的代码如下:

trait EntityMapper[+Entity] {
    def create[E <: Entity](e: E): E
}

object FooMapper extends EntityMapper[Foo] {
...
}

I have also tried achieving the same thing with simple inheritance but I cannot correctly restrict the type parameter in FooMapper to only take Foos, I have to make the method signature match the trait exactly which is why I started trying to implement it using generics with a type bound. 我也尝试过通过简单的继承来实现相同的目的,但是我不能正确地限制FooMapper中的类型参数仅采用Foos,我必须使方法签名与特征完全匹配,这就是为什么我开始尝试使用带有泛型的泛型来实现它的原因。类型绑定。 The code for that attempt is below: 该尝试的代码如下:

trait EntityMapper {
    def create(e: Entity): Entity
}

object FooMapper extends EntityMapper {
    def create(e: Foo): Foo = {
        println(e.id)
        e
    }
}

The error code returned is: 返回的错误代码为:

Test.scala:13: error: object creation impossible, since method create in trait EntityMapper of type (e: experiment.Entity)experiment.Entity is not defined Test.scala:13:错误:无法创建对象,因为未在类型(e:experiment.Entity)experiment.Entity的特征EntityMapper中创建方法

(Note that experiment.Entity does not match experiment.Foo: class Foo in package experiment is a subclass of trait Entity in package experiment, but method parameter types must match exactly.) (请注意experiment.Entity与experiment.Foo不匹配。Foo:包实验中的Foo类是包实验中特征Entity的子类,但方法参数类型必须完全匹配。)

object FooMapper extends EntityMapper {
       ^

Any help would be greatly appreciated. 任何帮助将不胜感激。 I'm using Scala version 2.10.3. 我正在使用Scala 2.10.3版本。

You can fix the error in a couple of ways 您可以通过以下几种方式修复错误

1.Specifying the generic type constraint on the trait. 1.在特征上指定通用类型约束。

trait EntityMapper[E <: Entity] {
  def create(e: E): E
}

object FooMapper extends EntityMapper[Foo] {
  def create(e: Foo): Foo = {
    println(e.id)
    e
  }
}

2.Use parameterized types 2.使用参数化类型

trait EntityMapper {
  type E <: Entity
  def create(e: E): E
}

object FooMapper extends EntityMapper {
  type E = Foo
  def create(e: Foo): Foo = {
    println(e.id)
    e
  }
}

Look at Scala: Abstract types vs generics to get some more background on the two approaches. 查看Scala:抽象类型与泛型,以获取有关这两种方法的更多背景知识。

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

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