繁体   English   中英

Scala:下限上下文泛型需要类型不匹配_$1

[英]Scala: Type mismatch required _$1 for lower context bound generics

我很难解决这种类型不匹配的编译错误

我有一系列的 Sources 想要实现策略模式来处理适当的类型

sealed trait Source
case class SourceA extends Source
case class SourceB extends Source

我的预处理器包含许多处理适当案例类的策略

trait SourceStrategy[T <: Source] {    
    def isApplicable(source: Source): Boolean    
    def preprocess(source: T): Whatever
}

这个想法是对于上面的每个案例类都将存在一个 SourceStrategy 实现

我想做这样的事情,通过适当的策略循环,找到适用的策略,然后调用该策略

class MyPreprocessor (strategies: Set[SourceStrategy[_ <: Source]]) {
  def performPreprocessing(source: Source) = {
    val preprocessor = strategies.filter(b => b.isApplicable(source)).head
    preprocessor.preprocess(source)
  }
}

我在最后一行中发现_$1Source不匹配。

我了解发生这种情况的原因,但我不确定如何以干净的方式解决它

您要么想在编译时检查当前策略是否适用于当前源,要么想在运行时检查这一点。

如果你在运行时这样做,那么让SourceStrategy泛型就没有多大意义

trait SourceStrategy {
  def isApplicable(source: Source): Boolean
  def preprocess(source: Source): Whatever
}
class MyPreprocessor (strategies: Set[SourceStrategy]) {
  def performPreprocessing(source: Source) = {
    val preprocessor = strategies.filter(b => b.isApplicable(source)).head
    preprocessor.preprocess(source)
  }
}

如果您在编译时这样做,那么您可以使用异构集合和类型类

trait PerformPreprocessing[L <: HList, T <: Source] {
  def performPreprocessing(strategies: L, source: T): Whatever
}
object PerformPreprocessing {
  implicit def recur[T <: Source, S, L <: HList, T1 <: Source](implicit
    performPreprocessing: PerformPreprocessing[L, T1]
  ): PerformPreprocessing[S :: L, T1] =
    (strategies, source) => performPreprocessing.performPreprocessing(strategies.tail, source)
  implicit def base[T <: Source, S <: SourceStrategy[T], L <: HList]: PerformPreprocessing[S :: L, T] =
    (strategies, source) => strategies.head.preprocess(source)
}

class MyPreprocessor[L <: HList](strategies: L) {
  def performPreprocessing[T <: Source](source: T)(implicit
    performPreprocessing: PerformPreprocessing[L, T]
  ): Whatever =
    performPreprocessing.performPreprocessing(strategies, source)
}

暂无
暂无

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

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