在 Scala 函式中定義型別引數串列時,它引入了可以在函式簽名中參考的型別變數,例如
def test[A,B,C] (a: A, f: (A,B) => C): B => C
我的問題可能是微不足道的,但我似乎無法弄清楚為什么
def sum[Int](l: List[Int]): Int = {
l.foldLeft(0)((a, b) => a b )
}
投擲
None of the overloaded alternatives of method in class Int with types
(x: Double): Double
(x: Float): Float
(x: Long): Long
(x: Int): Int
(x: Char): Int
(x: Short): Int
(x: Byte): Int
(x: String): String
match arguments ((b : Int))mdoc
而簡單地洗掉型別引數 [Int] 編譯完全沒問題。
def sum(l:List[Int]): Int = {
l.foldLeft(0)((x, y) => x y)
}
uj5u.com熱心網友回復:
在sum[Int](l: List[Int]): Int方法定義中,您實際上是在宣告一個名為的泛型型別引數Int,它僅與預定義型別的名稱匹配,但與其無關,因此編譯器無法找到有效的 方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/444250.html
標籤:斯卡拉
