我工作的一些功能,需要一個元素,與此元素返回單例類,參數化類型,如下圖所示:Scala和設置的詮釋
def singletonSet(elem: Int): Set =
e => elem == e
但斯卡拉REPL發送信號指示這樣的問題:
scala> def singletonSet(elem: Int): Set =
| e => elem == e
<console>:7: error: type Set takes type parameters
def singletonSet(elem: Int): Set =
我曾嘗試爲INT集添加一個類型,但它並不能幫助
def singletonSet(elem: Int): Set[Int] =
e => elem == e
和
scala> def singletonSet(elem: Int): Set[Int] =
| e => elem == e
<console>:8: error: type mismatch;
found : Int => Boolean
required: Set[Int]
e => elem == e
坦白地說,我不知道我的錯誤是鋪設,我使用2.11.4斯卡拉:)
編輯:
類似的問題,我此行的代碼:
def union(s: Set[Int], t: Set[Int]): Set[Int] = (e: Int) => s(e) || t(e)
<console>:7: error: type mismatch;
found : Int => Boolean
required: Set[Int]
def union(s: Set[Int], t: Set[Int]): Set[Int] = (e: Int) => s(e) || t(e)
這會導致類似的錯誤。
編輯:
我忘了
type Set = Int => Boolean
'Set [Int]'是'Int => Boolean'的子類型,所以你的直覺是相反的。 – 2014-12-13 18:58:10