2015-08-14 13 views
3

我試圖運行this math.stackexchange post中提供的Scala代碼(請參閱第二個答案),但似乎遇到了以implicit def...開頭的問題。編譯器告訴我error: expected start of definitionScala中的定義錯誤的預期開始

有什麼想法?謝謝!

我應該補充說我使用http://www.tutorialspoint.com/compile_scala_online.php來運行我的代碼。

+0

你確定你已經複製了整個代碼嗎?對我來說,這個例子完美無瑕。 –

+0

是的,我正在整個代碼,但不知何故仍然有錯誤。下面的解決方案工作,但現在我得到'類型日期=(字符串,詮釋)''預期的類或對象定義'錯誤以及'val ...'的每個實例... – stats134711

回答

5

剛剛在Scala REPL上嘗試過你的例子,它對我的​​預期效果。

移動implicit def一個對象:

object MyImplicits { 
    /** Pimp `Set[X]` with a few convenient operators */ 
    implicit def logicalSetOps[X](set: Set[X]) = new { 
    def and(other: Set[X]) = set.intersect(other) 
    def or(other: Set[X]) = set.union(other) 
    def minus(other: Set[X]) = set.filterNot(other.contains) 
    } 
} 

然後執行:

import MyImplicits._ 

這應該爲你工作。

+0

謝謝!這對我有用。但是,我遇到了另一個錯誤。我得到'類型Date =(String,Int)'的預期類或對象定義錯誤以及低於該值的'val ...'的每個實例... – stats134711

+1

將所有行從'type Date'移動到以@Sascha提到的方式結束擴展App的對象。編譯器只需要類,對象和特徵作爲開始定義。 –

+0

謝謝你的幫助! – stats134711

2

該示例中的代碼旨在被粘貼到工作表或REPL中。

還應努力將其粘貼裏面的

object MathApp extends App { 
    // paste here 
} 

然後你可以運行MathApp作爲階或Java應用程序。

+0

謝謝你的幫助! – stats134711