Reputation: 313
I am beginner of Scala, I try to make a class and use of it, which class has variables (var) and some processing (in def). I write class like as follows;
class Example() {
var ExampleVar0 = 0
var ExampleVar1 = 0
def ExampleProcessing(_arg0: Int, _arg1: Int) : (Int, Int) = {
ExampleVar0 = _arg0 + 1
ExampleVar1 = _arg1 - 1
(ExampleVar0*2, ExampleVar1/2)
}
}
I want to initialize ExampleVar*
and to keep and update the value. I use the class in other class;
var result0
var result1
val exp = Example()
exp.ExampleProcessing(5, 6)(result0, result1)
The both of result0 and result1 is zero, seems always write with zero by statement of the var ExampleVar0 = 0
. I checked the value of the var (println(exp.ExampleVar0.toString)
) then it is always zero. I might misunderstand about initialization of var.
Could you please point out where is my misunderstand and how to solve it?
Upvotes: 0
Views: 46
Reputation: 27421
You appear to be using result0
and result1
as method parameters, which does not look right. Try this:
val exp = Example()
val (result0, result1) = exp.ExampleProcessing(5, 6)
In this case the var
does not seem to be relevant or necessary, but I will give the usual warning that it is best to avoid var
and use immutable data structures.
Upvotes: 1