Reading http://www.scala-lang.org/docu/files/ScalaByExample.pdf
This piece of code :
def While (p: => Boolean) (s: => Unit) {
if (p) { s ; While(p)(s) }
}
Is given this explanation :
The While function takes as first parameter a test function, which
takes no parameters and yields a boolean value. As second parameter it
takes a command function which also takes no parameters and yields a
result of type Unit. While invokes the command function as long as the
test function yields true.
Where is if (p) evaluated to true or false ?
Should the function s not be declared somewhere ? There is no code for function s ?
Exactly there, in that line.
pandsare call-by-name parameters, because of the=>in front of them in the parameter lists of the methodWhile. Every time their name is used in the body ofWhile, they are evaluated.sis a parameter to theWhilemethod, just likep. (Why are you asking this question abouts, but not aboutp?). Methods and functions in Scala can have multiple parameter lists. TheWhilemethod has two parameter lists.You call this
Whilemethod by passing it something that evaluates toBoolean(the parameterp), and a block (the parameters).In this example
pisi < 5, a function that evaluates to aBoolean, andsis the block between the{and}.