val Array(k,s) = readLine.split(" ").map(_.toInt)
This code works fine. But not this:
val Array(k,S) = readLine.split(" ").map(_.toInt)
Capitalizing "s" here gives me an error: error: not found: value S
What is going on?
val Array(k,s) = readLine.split(" ").map(_.toInt)
This code works fine. But not this:
val Array(k,S) = readLine.split(" ").map(_.toInt)
Capitalizing "s" here gives me an error: error: not found: value S
What is going on?
When using extractors, symbols that begin with a lower case character will be interpreted as a variable to hold an extracted value. On the other hand, symbols that begin with an upper case character are used to refer to variables/values declared in an outer scope.
Another example:
val X = 2
something match {
case (X, y) => // matches if `something` is a pair whose first member is 2, and assigns the second member to `y`
case (x, y) => // matches if `something` is a pair, and extracts both `x` and `y`
}
When you are creating the
k
ands
identifiers withval Array(k,s) = ...
, you are using pattern matching to define them.From the Scala Specifications (1.1 Identifiers):
That is, when you say
val Array(k,S) = ...
, you're actually matchingS
against a constant. Since you have noS
defined, Scala reportserror: not found: value S
.Note that Scala will throw a
MatchError
if the constant is defined but it still cannot find a match :