我在学习scala,这个问题可能很愚蠢,但是......为什么?scala:type mismatch error - found T,required字符串
例如,这是确定的:
def matchList(ls: List[Int]): List[Int] = ls match {
case 1 :: rest => rest
case a :: b :: rest => (a + b) :: rest
case _ => ls
}
matchList: (ls: List[Int])List[Int]
但随着类型参数的函数不能编译:
def matchList[T](ls: List[T]): List[T] = ls match {
case 1 :: rest => rest
case a :: b :: rest => (a + b) :: rest
case _ => ls
}
<console>:10: error: type mismatch;
found : T
required: String
case a :: b :: rest => (a + b) :: rest
为什么?
他是否一定希望手术是联想? – huitseeker 2011-12-22 21:19:47
他没有具体说明。而且我不能想到结合性会伤害他的问题的情况。 – missingfaktor 2011-12-23 04:27:23
你是对的,它不会:'matchList'组合最多两个元素,所以关联性是无关紧要的。 “这只是一个提醒,Semigroup的规格比'T - > T - > T'类型的规格要小。 :) – huitseeker 2011-12-23 05:52:43