以下是可以正常使用的函数定义:
case class Type(unique:String, tech:String)
case class TVal(tech:String, value:String)
def f1(techName:String, variables: List[TVal]):String =
variables.filter(variable =>
techName == variable.tech)
.map(variable => variable.value) match {
case Nil => "empty list"
case head::Nil => head
case head::tail => "more than one"
}
def f2(defList:List[Type],
variables: List[TVal]):List[Tuple2[String,String]] =
defList.map(t => {
t.unique -> f1(t.tech, variables)
})
def comp(defList:List[Type],
variables: List[TVal]):Map[String,String] =
Map(f2(defList, variables): _*)
现在,我尝试将每个函数调用替换为其主体:
def compSubstitution(defList:List[Type],
variables: List[TVal]):Map[String,String] =
Map(defList.map(t => {
t.unique -> variables.filter(variable =>
t.tech == variable.tech)
.map(variable => variable.value) match {
case Nil => "empty list"
case head::Nil => head
case head::tail => "more than one"
}
}): _*)
现在我遇到编译错误:
Error:(33, 14) pattern type is incompatible with expected type;
found : scala.collection.immutable.Nil.type
required: (String, List[String])
case Nil => "empty list"
Error:(33, 14) type mismatch;
found : scala.collection.immutable.Nil.type
required: (String, List[String])
case Nil => "empty list"
Error:(34, 18) constructor cannot be instantiated to expected type;
found : scala.collection.immutable.::[B]
required: (String, List[String])
case head::Nil => head
Error:(35, 18) constructor cannot be instantiated to expected type;
found : scala.collection.immutable.::[B]
required: (String, List[String])
case head::tail => "more than one"
Error:(29, 20) type mismatch;
found : List[String]
required: Seq[(String, String)]
Map(defList.map(t => {
怎么可能?你能描述一下吗?
答案 0 :(得分:3)
您正在将类型(字符串->列表[字符串])与Nil进行匹配,并且您只想对列表进行匹配?
所以我认为您只缺少括号:
def compSubstitution(defList:List[Type], variables: List[TVal]):Map[String,String] =
Map(defList.map(t => {
t.unique ->(
variables.filter(variable => t.tech == variable.tech)
.map(variable => variable.value) match {
case Nil => "empty list"
case head::Nil => head
case head::tail => "more than one"
})
}): _*)