第一個stackoverflow問題和新的斯卡拉。想了解多態類型與這個例子中,我寫道:Scala類型不匹配:Nothing => Nothing?
def identFun[A](a: A): A = a
def testerChar(l: List[Char], f: Char => Char): List[Char] = {
val li = l.map((r: Char) => f(r: Char))
li
}
工作正常(如果不是有點冗長):
scala> testerChar(List('a','b','c'), identFun)
res49: List[Char] = List(a, b, c)
但是:
def testerA[A](l: List[A], f: A => A): List[A] = {
val li = l.map((r: A) => f(r: A))
li
}
產量:
scala> testerA(List('a','b','c'), identFun)
<console>:14: error: type mismatch;
found : Nothing => Nothing
required: Char => Char
testerA(List('a','b','c'), identFun)
我在想什麼t將允許「testerA」返回傳遞給它的任何類型的身份?
謝謝!
嘗試:
def testerA[A](l: List[A])(f: A => A): List[A] = {
val li = l.map((r: A) => f(r: A))
li
}
了:
scala> testerA(List('a','b','c'), identFun)
<console>:14: error: too many arguments for method testerA: (l: List[A])(f: A => A)List[A]
testerA(List('a','b','c'), identFun)
將其更改爲'def testerA [A](l:List [A])(f:A => A)' –
或者傳遞類型。 testerA [Char](List('a','b','c'),identFun) – sascha10000
curried'def'應該像這樣調用:'testerA(List(...))(identFun)',使用''(''代替'',''' – Clashsoft