If I declare a val 1
how can I get access to it without changing name val 1
?
val 1 = ONE
def ONE = 1
print(1) // `1` should be a reference to ONE which I declared above.
This does not declare a variable with the name 1
:
val 1 = ONE
What it does instead is pattern matching - and the result is not useful.
Try this, and you'll get a pattern match error:
def ONE = 2
val 1 = ONE
Variables in Scala cannot have names that consists of only digits, or that start with a digit. You cannot declare a variable with the name 1
.
Like Jesper said it's pattern matching. Consider this code which is similar to yours:
def ONE_AND_TWO = (1, 2)
val (1, b) = ONE_AND_TWO
println(b) // prints 2
val (2, _) = ONE_AND_TWO //scala.MatchError: (1,2) (of class scala.Tuple2$mcII$sp)
In your case you have just single value instead of tuple. If you however want to name variable, value or function as close to 1
as possible you can use:
def `1` = ONE