println
方法需要一個字符串,並且您已經向pthr提供了一個帶有+
運算符的字符串。爲了將pthr轉換爲字符串,編譯器需要知道它是哪種類型。你沒有在你的pthr聲明中提供一個類型,而是讓它被推斷出來。無論出於何種原因,推理都不起作用,因此編譯器正在請求顯式類型聲明。你可以簡單地做到這一點:
val pthr : Set[String] = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet
這應該照顧它。
但是,我無法用Scala 2.11.2重現您的結果。我得到:
比使用
toSet
方法作爲後綴運算符的
scala> val pthr = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet
<console>:7: warning: postfix operator toSet should be enabled
by making the implicit value scala.language.postfixOps visible.
This can be achieved by adding the import clause 'import scala.language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scala docs for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
val pthr = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet
^
pthr: scala.collection.parallel.immutable.ParSet[String] = ParSet(Thread[ForkJoinPool-1-worker-29,5,main], Thread[ForkJoinPool-1-worker-19,5,main], Thread[ForkJoinPool-1-worker-13,5,main], Thread[ForkJoinPool-1-worker-3,5,main], Thread[ForkJoinPool-1-worker-27,5,main], Thread[ForkJoinPool-1-worker-5,5,main], Thread[ForkJoinPool-1-worker-1,5,main], Thread[ForkJoinPool-1-worker-23,5,main], Thread[ForkJoinPool-1-worker-15,5,main], Thread[ForkJoinPool-1-worker-11,5,main], Thread[ForkJoinPool-1-worker-21,5,main], Thread[ForkJoinPool-1-worker-9,5,main], Thread[ForkJoinPool-1-worker-7,5,main], Thread[ForkJoinPool-1-worker-17,5,main], Thread[ForkJoinPool-1-worker-31,5,main], Thread[ForkJoinPool-1-worker-25,5,main])
scala> println("thread = " + pthr)
thread = ParSet(Thread[ForkJoinPool-1-worker-29,5,main], Thread[ForkJoinPool-1-worker-19,5,main], Thread[ForkJoinPool-1-worker-13,5,main], Thread[ForkJoinPool-1-worker-3,5,main], Thread[ForkJoinPool-1-worker-27,5,main], Thread[ForkJoinPool-1-worker-5,5,main], Thread[ForkJoinPool-1-worker-1,5,main], Thread[ForkJoinPool-1-worker-23,5,main], Thread[ForkJoinPool-1-worker-15,5,main], Thread[ForkJoinPool-1-worker-11,5,main], Thread[ForkJoinPool-1-worker-21,5,main], Thread[ForkJoinPool-1-worker-9,5,main], Thread[ForkJoinPool-1-worker-7,5,main], Thread[ForkJoinPool-1-worker-17,5,main], Thread[ForkJoinPool-1-worker-31,5,main], Thread[ForkJoinPool-1-worker-25,5,main])
其他,沒有什麼錯的代碼,它編譯並正確執行。也許你應該檢查你的編譯器版本?
使用它與點,沒有空格和...問題解決 – billpcs 2014-11-02 15:16:26
什麼意思,「這引發:」錯誤:遞歸值pthr需要類型「 ?你是說你正在執行該代碼時正在拋出一個異常,還是你說編譯器正在「拋出」這樣的錯誤? – 2014-11-02 15:19:17