4

以下 Scala 代码有效:

object ReducerTestMain extends App {

  type MapOutput = KeyVal[String, Int]

  def mapFun(s:String): MapOutput = KeyVal(s, 1)

  val red = new ReducerComponent[String, Int]((a: Int, b: Int) => a + b)

  val data = List[String]("a", "b", "c", "b", "c", "b")

  data foreach {s => red(mapFun(s))}
  println(red.mem)
  // OUTPUT: Map(a -> 1, b -> 3, c -> 2)
}

class ReducerComponent[K, V](f: (V, V) => V) {
  var mem = Map[K, V]()

  def apply(kv: KeyVal[K, V]) = {
    val KeyVal(k, v) = kv
    mem += (k -> (if (mem contains k) f(mem(k), v) else v))
  }
}

case class KeyVal[K, V](key: K, value:V)

我的问题是我想像这样实例化ReducerComponent

val red = new ReducerComponent[MapOutput, Int]((a: Int, b: Int) => a + b)

甚至更好:

val red = new ReducerComponent[MapOutput](_ + _)

这意味着很多事情:

  1. 我想对类型进行类型MapOutput检查KeyVal[K, C]
  2. 我想检查C是否与 中使用的类型相同f
  3. 我还需要“提取”K以便memapply.

要问的很多吗?:) 我想写类似的东西

class ReducerComponent[KeyVal[K,V]](f: (V, V) => V) {...}

到我将实例化的时候,ReducerComponent我所拥有的只是fand MapOutput,所以推断 V 是可以的。但是我只有KeyVal[K,V]一个类的类型参数,它可以不同于KeyVal[_,_].

我知道如果您了解类型推断的工作原理,我的要求可能很疯狂,但我不知道!而且我什至不知道什么是继续的好方法——除了在我的高级代码中一直进行显式类型声明。我应该改变所有的架构吗?

4

2 回答 2

5

只写一个简单的工厂:

case class RC[M <: KeyVal[_, _]](){
   def apply[K,V](f: (V,V) => V)(implicit ev: KeyVal[K,V] =:= M) = new ReducerComponent[K,V](f)
}

def plus(x: Double, y: Double) = x + y

scala> RC[KeyVal[Int, Double]].apply(plus)
res12: ReducerComponent[Int,Double] = ReducerComponent@7229d116

scala> RC[KeyVal[Int, Double]]()(plus)
res16: ReducerComponent[Int,Double] = ReducerComponent@389f65fe

如您所见,ReducerComponent具有适当的类型。这里使用隐含的证据来捕获KV从您的M <: KeyVal[_, _].

PS 上面的版本需要为您明确指定参数类型f,例如(_: Double) + (_: Double). 如果你想避免这种情况:

case class RC[M <: KeyVal[_, _]](){
   def factory[K,V](implicit ev: KeyVal[K,V] =:= M) = new {
      def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
   }
}

scala> RC[KeyVal[Int, Double]].factory.apply(_ + _)
res5: ReducerComponent[Int,Double] = ReducerComponent@3dc04400 


scala> val f = RC[KeyVal[Int, Double]].factory
f: AnyRef{def apply(f: (Double, Double) => Double): ReducerComponent[Int,Double]} = RC$$anon$1@19388ff6

scala> f(_ + _)
res13: ReducerComponent[Int,Double] = ReducerComponent@24d8ae83

更新。如果你想泛化 keyval - 使用 type 函数:

type KV[K,V] = KeyVal[K,V] //may be anything, may implement `type KV[K,V]` from some supertrait

case class RC[M <: KV[_, _]](){   
  def factory[K,V](implicit ev: KV[K,V] =:= M) = new {
    def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
  }
}

但请记住,apply从你的问题仍然需要KeyVal[K,V].

你也可以传入KV一些类:

class Builder[KV[_,_]] {
  case class RC[M <: KV[_, _]](){   
    def factory[K,V](implicit ev: KV[K,V] =:= M) = new {
      def apply(f: (V,V) => V) = new ReducerComponent[K,V](f)
    }
  }
}

scala> val b = new Builder[KeyVal]
scala> val f = b.RC[KeyVal[Int, Double]].factory
scala> f(_ + _)
res2: ReducerComponent[Int,Double] = ReducerComponent@54d9c993
于 2015-04-20T19:21:54.417 回答
4

为此,您将需要依赖路径的类型。我推荐以下内容:

首先,编写一个将您的相关类型作为成员的特征,以便您可以在定义中访问它们:

trait KeyValAux {
  type K
  type V
  type KV = KeyVal[K, V]
}

现在您可以为以下内容创建工厂ReducerComponent

object ReducerComponent {
  def apply[T <: KeyValAux](f: (T#V, T#V) => T#V) =
    new ReducerComponent[T#K, T#V](f)
}

请注意,在这里,我们可以简单地访问该类型的成员。我们不能对类型参数执行此操作。

现在,定义您MapOutput的术语KeyValAux(也许另一个名称更适合您的用例):

type MapOutput = KeyValAux { type K = String; type V = Int }

def mapFun(s:String): MapOutput#KV = KeyVal(s, 1)

val red = ReducerComponent[MapOutput](_ + _)

更新

正如@dk14 在评论中提到的,如果您仍然想要类型参数语法,您可以执行以下操作:

trait OutputSpec[KK, VV] extends KeyValAux {
  type K = KK
  type V = VV
}

然后你可以写:

type MapOutput = OutputSpec[String, Int]

或者,您可以编写OutputSpec为类型函数:

type OutputSpec[KK, VV] = KeyValAux { type K = KK; type V = VV }

这不会生成额外的未使用类。

于 2015-04-20T19:18:19.650 回答