我定义了3个案例类,如下所示:
case class Func1(inputA: HashMap[String, String], mapFn: (String, String) => List[(String, Int)], redFn: (String, List[Int]) => (String, Int))
case class Func2(inputB: HashMap[String, String], mapFn: (String, String) => List[(String, String)], redFn: (String, List[String]) => (String, List[String]))
case class Func3(inputC: HashMap[String, String], mapFn: (String, String) => List[(String, String)], redFn: (String, List[String]) => (String, Int)) 在receive方法中,我有与上面匹配的相应案例:
case Func1(inputA, mapFn, redFn) => // Do something
case Func2(inputB, mapFn, redFn) => // Do something
case Func3(inputC, mapFn, redFn) => // Do something是否可以用一个通用的case类替换这3个case类?
我尝试了以下几种方法:
我将case类定义如下:
case class Func[K, V, X, Y, U](input: HashMap[K, V], mapFn: (K, V) => List[(X, Y)], redFun: (X, List[Y]) => (X, U))然后,在MyActor类的接收方法中,我有:
case Func[K, V, X, Y, U](input: HashMap[K, V], mapFn: (K, V) => List[(X, Y)], redFun: (X, List[Y]) => (X, U)) => //code here编译错误如下所示。第一个错误指示错误位于mapFn: (K,V) => List[(X,Y)]中的=>
')' expected but '=>' found.
[error] case MapIt[K, V, X, Y, U](input: HashMap[K, V], mapFn: (K,V) => List[(X, Y)], redFun: (X, List[Y]) => (X, U)) =>第二个错误表明问题出在"mapFn:(K,V) => List(X,Y)“的最后一个方括号中(逗号之前)。
'=>' expected but ']' found.
[error] case Func[K, V, X, Y, U](input: HashMap[K, V], mapFn: (K, V) => List[(X, Y)], redFun: (X, List[Y]) => (X, U)) =>发布于 2017-05-29 15:54:23
如果这有帮助,请告诉我。
case class FuncG[K, V, X, Y, U](input: HashMap[K, V], mapFn: (K, V) => List[(X, Y)], redFun: (X, List[Y]) => (X, U))
val firstParam: HashMap[String, String] = HashMap("test key" -> "test value")
def mapfn1(a1:String, a2:String):List[(String, Int)] = List(("val1",4))
def mapfn2(a1:String, a2:String):List[(String, String)] = List(("val2","val3"))
def redfn1(r1:String, r2:List[Int]):(String, Int) = ("val4",56)
def redfn2(r1:String, r2:List[String]):(String, List[String]) = ("val4",List("val5"))
def redfn3(r1:String, r2:List[String]):(String, Int) = ("val6",34)
val pattern1 = FuncG(firstParam,mapfn1,redfn1)
val pattern2 = FuncG(firstParam,mapfn2,redfn2)
val pattern3 = FuncG(firstParam,mapfn2,redfn3)
def doMatch(pattern:Any) = {
pattern match {
case FuncG(inputA, mapFn, redFun) => println("Found")
}
}
doMatch(pattern1)
doMatch(pattern2)
doMatch(pattern3)https://stackoverflow.com/questions/44229419
复制相似问题