This piece of Scala code demonstrates two distinct ways to pass arguments to functions: by-value and by-name, as well as basic function definition and invocation.
Here's an explanation of what each part of this code does:
Imports
import scala.util.Random
This imports the Random utility class from the Scala standard library, which provides methods for generating random numbers.
By-Value Function
def byValue(t: Int): Seq[Int] =
for (_ <- 0 until 3)
yield t
This defines a function byValue that takes an integer t as an argument (passed by value) and returns a sequence of 3 integers. The same value of t is used for all three elements because it is computed before the function is called, and the evaluated value is passed to the function.
By-Name Function
def byName(t: => Int): Seq[Int] = for (_ <- 0 until 3) yield t
This defines a function byName that takes an integer t as a by-name parameter. A by-name parameter is only evaluated when it is used within the function. Therefore, each time t is accessed within the yield statement, it is recomputed, potentially giving a different result if t involves a computation with side effects, like generating a random number.
A Lazy Evaluation Function
def helloAll = {
Math.random().toString
}
This defines a function helloAll that, when called, returns a new random number as a string. It's a no-argument function, which means you call it without parentheses—in Scala, this is a way to define a function that behaves more like a lazy value.
A Higher-Order Function
def callAll(runMe: => String) = {
println(runMe)
}
This defines a function callAll which takes a by-name parameter runMe of type String. Because it's a by-name parameter, runMe is not evaluated until it's used within the callAll function, meaning that each call to runMe will execute the underlying computation again (if there is any computation).
Main Function
@main def helloMe() = {
// Code commented out; it demonstrates by-value vs. by-name parameter differences.
/*
println(
byValue(Random.nextInt(10000))
) // : Seq[Int] = Seq(2551, 2551, 2551)
println(byqName(Random.nextInt(10000)))
*/
callAll(helloAll) // Calls 'helloAll', prints a random number as a string.
callAll(helloAll) // Calls 'helloAll' again, prints another random number as a string.
}
This annotation @main tells the Scala compiler that this is the entry point into the program. The function helloMe is the main function that the runtime will call when executing the code. Inside this function, the callAll function is called twice with helloAll as an argument. Since runMe is a by-name parameter, each call to callAll will invoke helloAll anew, producing and printing a new random number string each time.
Commented-out code, which is not executed, would demonstrate the difference between by-value and by-name parameters using Random.nextInt(10000) which generates a random integer between 0 and 9999. If uncommented, the byValue call would print three identical random numbers (since the number is generated once and passed by value to the function), whereas the byName call would print a sequence of three potentially different random numbers (since the random number generator is invoked anew each time it's needed by the function).
Note that there's a typo in the commented code: byqName should be byName.