可以帮助我了解所使用的代码和运算符类型,或者可以在这里使用
def times [A](f: =>A): Unit={
def loop(current: Int): Unit=
if(current > 0){
f
loop(current - 1)
}
loop(x)
}
答案 0 :(得分:1)
def times [A](f: =>A): Unit={ // f is call-by-name argument
def loop(current: Int): Unit // nested function inside of function types
此代码执行f
x
次(我希望在您的代码中定义此变量)。基本上f
是一个函数,它将在给定(x
)次数的情况下执行。
要详细了解按名称呼叫:Call by name vs call by value in Scala, clarification needed