我是Peter Pilgrim。我看到Martin Odersky在Scala中創建了一個控制抽象。不過,我似乎還不能在IntelliJ IDEA 9中重複它。它是IDE嗎?一個人如何讓Scala控制抽象重複直到?
package demo
class Control {
def repeatLoop (body: => Unit) = new Until(body)
class Until(body: => Unit) {
def until(cond: => Boolean) {
body;
val value: Boolean = cond;
println("value="+value)
if (value) repeatLoop(body).until(cond)
// if (cond) until(cond)
}
}
def doTest2(): Unit = {
var y: Int = 1
println("testing ... repeatUntil() control structure")
repeatLoop {
println("found y="+y)
y = y + 1
}
{ until (y < 10) }
}
}
錯誤消息讀取:
Information:Compilation completed with 1 error and 0 warnings
Information:1 error
Information:0 warnings
C:\Users\Peter\IdeaProjects\HelloWord\src\demo\Control.scala
Error:Error:line (57)error: Control.this.repeatLoop({
scala.this.Predef.println("found y=".+(y));
y = y.+(1)
}) of type Control.this.Until does not take parameters
repeatLoop {
在咖喱函數主體可以被認爲以返回一個表達式(y的值+ 1)然而repeatUntil的聲明機體參數明確地說,這可以忽略不計?
錯誤是什麼意思?
不錯。我喜歡匿名函數對象。 – 2010-06-14 12:18:25
它被稱爲結構類型 – 2010-06-14 13:38:31
爲什麼嘗試/抓住,當你可以'做{body} while(!condition)'? – 2010-06-14 14:44:57