2011-02-15 36 views
0

當演員中使用Console.withOut時,我發現了一些奇怪的行爲。下面的代碼:Scala演員+ Console.withOut可能的bug

case object I 
val out = new PipedOutputStream 
val pipe = new PipedInputStream(out) 
def read: String = ** read from `pipe` stream 
class A extends Actor{ 
    var b: Actor = _ 
    Console.withOut(out){ 
    b = actor { loop { self react { 
      case I => println("II") 
     }}} 
    } 
    def act = { 
    loop { self react { 
     case I => 
      println("I") 
      b ! I 
     }} 
    } 
    } 
def main(args: Array[String]): Unit = { 
    val a = new A 
    a.start 
    a ! I 
    Thread sleep 100 
    println("!!\n" + read + "!!") 
    } 

得到了以下的輸出:

!! 
I 
II 
!! 

任何想法,爲什麼從A演員的act方法輸出也重定向?謝謝您的回答。

UPDATE: 這裏讀功能:

@tailrec 
    def read(instream: InputStream, acc: List[Char] = Nil): String = 
    if(instream.available > 0) read(instream, acc :+ instream.read.toChar) else acc mkString "" 
    def read: String = read(pipe) 
+0

你還可以發佈你從`pipe`讀取的代碼嗎? – axel22 2011-02-15 09:00:40

回答

3

在我看來,與此相反,既不演員其輸出重定向,因爲withOut將完成執行println("II")被稱爲很久以前。因爲這全都是基於DynamicVariable,所以我不願意賭它。 :-)缺乏工作代碼也排除了任何測試。

+0

同意。也不應該可見。我的想法是,`read`的代碼確實不應該這樣做,因此打印。 – axel22 2011-02-15 12:14:23