我試圖用一個匹配替換我的isInstanceOf檢查,但它不起作用。斯卡拉匹配錯誤
在我的方法中,我檢查一個樹節點 - 如果它是一片樹葉 - 我想立即將它返回到一個Vector中,如果不是,我繼續使用該方法。
所以我原本:
//code here
if (common.isInstanceOf[LeafNode]) {
return Vector(common.asInstanceOf[LeafNode].data)
}
//code here
然後我試圖取代它:
//code here
common match {
case leaf: LeafNode => return Vector(leaf.data)
}
//code here
,但我得到scala.MatchError。
約回報好一點,謝謝其實 – drozzy 2011-02-15 06:31:38
,如果你在你的函數返回語句,斯卡拉2.9編譯器會強迫你明確說明函數的返回類型。不知道有關早期版本,雖然 – Aaron 2011-08-18 20:55:40