By Alvin Alexander. Last updated: March 24, 2020
As one note and example of how to avoid using var
fields in Scala, I initially wrote this code:
var numDaysToProcess = 0
if (args.length != 0) {
numDaysToProcess = args(0).toInt
}
After I looked at that code for a moment I realized that I could write it without a var
like this instead:
val numDaysToProcess =
if (args.length == 0) {
0
} else {
args(0).toInt
}
That’s a simple example of one situation where you can easily avoid using a var
field in Scala. I’ll share more examples as they come up.