Topic | Using Properties/Fields as Method/Definitions in Scala Class/Object |
---|---|
Git sample | VarAndValTest.scala |
-
Thanks to functional nature of Scala :
var
orval
can be used as adef
-
Focus on following three lines from program. You will see a
var
, aval
and adef
, actually doing same job
/* A variable, value and definition, doing same jobs (increment of integer value) */
var incrementVar = (x: Int) => x + 1 /* A definition using 'var' */
val incrementVal = (x: Int) => x + 1 /* A definition using 'val' */
def incrementDef = (x: Int) => x + 1 /* A definition using 'def' */
/* Everyone will print '2' */
println(incrementVar(1))
println(incrementVal(1))
println(incrementDef(1))