2014-11-02 85 views
1

我從代碼「斯卡拉深入」,在交互式編輯器的工作原理:斯卡拉平行設置遞歸值需要類型

(1 to 1000).par map { _ => Thread.currentThread.toString } toSet 

這將打印一組與用於此並行運行的線程。所有的偉大工程,結果類型爲PARSET [字符串]

但後來我嘗試在代碼中使用它,我有這樣的:

val pthr = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet 
//val x = pthr.toSet 
println("thread = " + pthr) 

此拋出:「錯誤:PTHR需要類型的遞歸值」在println行。另一個觀察是,當我取消註釋第二行一切工作正常,結果是ParSet()。

這是怎麼回事?什麼是一種變量「pthr」?

+0

使用它與點,沒有空格和...問題解決 – billpcs 2014-11-02 15:16:26

+0

什麼意思,「這引發:」錯誤:遞歸值pthr需要類型「 ?你是說你正在執行該代碼時正在拋出一個異常,還是你說編譯器正在「拋出」這樣的錯誤? – 2014-11-02 15:19:17

回答

0

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]) 

其他,沒有什麼錯的代碼,它編譯並正確執行。也許你應該檢查你的編譯器版本?

0

是的我知道這個工程在交互式scala,但不在編譯期間scalac

好這裏是簡約的例子,讓我們把這個文件「test.scala」:

//test.scala 
object test { 
    def main(args: Array[String]) = { 
    val pthr = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet 
    println("thread = " + pthr) 
    } 
} 

現在,讓我們在終端執行:

-> scalac test.scala            
test.scala:5: error: recursive value pthr needs type 
println("thread = " + pthr) 
        ^
one error found 

-> scalac -version        
Scala compiler version 2.11.2 -- Copyright 2002-2013, LAMP/EPFL 

所以我又只能用scalac編譯時有這個問題。

當我添加一個類型PTHR(val pthr: Set[String] = ...),那麼一個錯誤是:

test.scala:4: error: type mismatch; 
found : Boolean 
required: Set[String] 
    val pthr:Set[String] = (1 to 1000).par map { _ => Thread.currentThread.toString } toSet 
                       ^
one error found 

不知道是怎麼回事:/

BTW。這是從「深度在斯卡拉」頁205複製的確切代碼。