Scala:"class type required but {trait} with {trait} found" 从 mixin 类型别名继承时

Scala: "class type required but {trait} with {trait} found" when inheriting from mixin type alias

我定义了一个非常常见的类型别名:

package object policy {

  type KeyGen[K] = Function0[K] with Serializable
}

但是当我尝试从它继承时:

import java.security.Key
case class FixedKeyGen(key: Key) extends KeyGen[Key] {

  override def apply(): Key = key
}

maven 编译器给我以下错误:

[ERROR] /home/peng/git/datapassport/core/src/main/scala/com/schedule1/datapassport/policy/ValueMapping.scala:16: class type required but () => java.security.Key with Serializable found
[ERROR] case class FixedKeyGen(key: Key) extends KeyGen[Key] {
[ERROR]                                          ^
[ERROR] /home/peng/git/datapassport/core/src/main/scala/com/schedule1/datapassport/policy/ValueMapping.scala:16: com.schedule1.datapassport.policy.KeyGen[java.security.Key] does not have a constructor
[ERROR] case class FixedKeyGen(key: Key) extends KeyGen[Key] {

这是怎么回事?

我认为您不能像那样直接扩展复合类型。也就是说,Function0[K] with Serializable 本身并不是 class 类型。它是一个没有构造函数的复合类型,这就是关键。在没有构造函数的情况下扩展某些东西真的没有意义。类型别名的作用与此类似(注意类型周围的括号):

case class FixedKeyGen(key: Key) extends (Function0[Key] with Serializable) {
    override def apply(): Key = key
}

我们得到同样的错误:

<console>:20: error: class type required but () => java.security.Key with Serializable found
       case class FixedKeyGen(key: Key) extends (Function0[Key] with Serializable) {

这是因为 Function0[Key] with Serializable 不是 class 类型。

但是,如果我删除括号,这当然有效。没有它们,FixedKeyGen 正在扩展 Function0 并混合 Serializable。有了它们,它正在尝试扩展复合类型。

要解决此问题,您可能只想使用特征,而不是:

trait KeyGen[K] extends Function0[K] with Serializable

case class FixedKeyGen(key: Key) extends KeyGen[Key] {
    override def apply(): Key = key
}