scala> val a, b, c = 1val a: Int = 1val b: Int = 1val c: Int = 1
scala> val a, b, c = util.Random.nextInt()
val a: Int = 2127446030val b: Int = 1199584050val c: Int = 638157382
/** Reads an int value from an entire line of the default input. * * @return the Int that was read * @throws java.io.EOFException if the end of the * input stream has been reached. * @throws java.lang.NumberFormatException if the value couldn't be converted to an Int */def readInt(): Int = {
val s = readLine()
if (s == null)
thrownew java.io.EOFException("Console has reached end of input")
else
s.toInt
}
By-name parameters are evaluated every time they are used. They won’t be evaluated at all if they are unused. This is similar to replacing the by-name parameters with the passed expressions.
Array.fill の内部実装は以下の通りです。
/** Returns an array that contains the results of some element computation a number * of times. * * Note that this means that `elem` is computed a total of n times: * {{{ * scala> Array.fill(3){ math.random } * res3: Array[Double] = Array(0.365461167592537, 1.550395944913685E-4, 0.7907242137333306) * }}} * * @param n the number of elements desired * @param elem the element computation * @return an Array of size n, where each element contains the result of computing * `elem`. */ def fill[T: ClassTag](n: Int)(elem: => T): Array[T] = {
if (n <= 0) {
empty[T]
} else {
val array = newArray[T](n)
var i = 0while (i < n) {
array(i) = elem
i += 1
}
array
}
}
コメントや例が書いてあり、丁寧だと感じました。
Note that this means that elem is computed a total of n times