我想學會用Scala編程,和我遇到有關underscore.Here一些問題是代碼:I(初學者)的感覺強調是混亂
FileMatcher.filesContaining("hello").foreach(println(_))
FileMatcher.filesContaining("hello").foreach(println((_:java.io.File).getName))
我的問題是,如果下劃線代表函數的形式參數(對嗎?,我已經看到了關於下劃線的一些答案),爲什麼第一個代碼運行良好,但第二個崩潰?
============================================== =======================
整個代碼(對不起,我英文不好。 「嘩啦啦」 的意思編譯失敗)
object FileMatcher{
private def filesHere = (new java.io.File(".")).listFiles
def filesMatching(query: String, matcher: (String, String) => Boolean) =
for (file <- filesHere if matcher(file.getName, query))
yield file
def filesEnding(query: String) =
filesMatching(query, _.endsWith(_))
def filesContaining(query: String) =
filesMatching(query, _.contains(_))
}
FileMatcher.filesContaining("hello").foreach(println(_))
FileMatcher.filesContaining("hello").foreach(println((_:java.io.File).getName))
你的意思是'墜毀'?你能解釋更多的錯誤嗎?問題標題也應該改變,以更清楚地反映問題。 – Jus12
@Kermit嘗試使用適當的語法並在逗號後面使用空格,以便用戶不會跳過您的問題。 – Glubus
什麼是'FileMatcher'?請包含所有信息以重現問題。 –