It is considered a good practice to avoid using mutable variables in Scala.
From "Programming in Scala, 2nd Edition", page 52: "Scala allows you to program in an imperative style, but encourages you to adopt a more functional style" and later "Scala encourages you to lean towards vals, but ultimately reach for the best tool given the job at hand."
How do you make this imperative construct elegant in Scala (focusing on variable count):
var count = 0
val foo = getRequest()
if(foo.isJsonObject){
count = doSomething(foo)
}
if(count>0){
specialCall()
} else{
defaultCall()
}
What construct or pattern do you use to transform your code with an imperative style to a functional style? Do you make use of the Option class or some other constructs?
if,try,match) as expressions that evaluate to result values but in Scala and other similar languages they are.