7

I declared a variable outside the function like this:

var s: Int = 0

passed it such as this:

def function(s: Int): Boolean={

   s += 1

   return true

}

but the error lines wont go away under the "s +=" for the life of me. I tried everything. I am new to Scala btw.

Alex Yarmula
  • 10,477
  • 5
  • 33
  • 32
J L
  • 367
  • 1
  • 5
  • 11
  • 1
    possible duplicate of [Can Scala call by reference?](http://stackoverflow.com/questions/4790050/can-scala-call-by-reference) – om-nom-nom Mar 29 '13 at 14:48
  • 1
    why would you want to increment the value in a function? Just return the new value. You are reassigning the value you passed in. – Boris the Spider Mar 29 '13 at 14:49
  • i am doing something else with the value. this is just a small example of what my problem was. – J L Mar 29 '13 at 14:51
  • You cannot do this, you are really calling `s = s + 1` so you are simply reassigning your local pointer to a new variable. – Boris the Spider Mar 29 '13 at 14:52
  • 1
    @JL all you can do in *this particular case* is to wrap int into say MutableInt container, that has operation increment and then pass it to function and invoke that method. You might think that this is a lot of overhead and indeed it is: you're going straight against language concepts. – om-nom-nom Mar 29 '13 at 14:54
  • return it, you can use a tuple. – Boris the Spider Mar 29 '13 at 14:59
  • J L, you might consider reviewing [this answer](http://stackoverflow.com/questions/1791408/what-is-the-difference-between-a-var-and-val-definition-in-scala) for why `val` (and immutability in general) is preferable to `var`. – Ryan Mar 29 '13 at 15:48

5 Answers5

4

First of all, I will repeat my words of caution: solution below is both obscure and inefficient, if it possible try to stick with values.

implicit class MutableInt(var value: Int) {
  def inc() = { value+=1 } 
}

def function(s: MutableInt): Boolean={
   s.inc() // parentheses here to denote that method has side effects
   return true
}

And here is code in action:

scala> val x: MutableInt = 0 
x: MutableInt = MutableInt@44e70ff

scala> function(x)
res0: Boolean = true

scala> x.value
res1: Int = 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
  • 7
    @JL I hope you're not going to seriously use this "solution" for anything, and I don't think that was om-nom-nom's intention. – Jesper Mar 29 '13 at 15:36
3

If you just want continuously increasing integers, you can use a Stream.

val numberStream = Stream.iterate(0)(_ + 1).iterator

That creates an iterator over a never-ending stream of number, starting at zero. Then, to get the next number, call

val number: Int = numberStream.next
Ron Romero
  • 9,211
  • 8
  • 43
  • 64
1

I have also just started using Scala this was my work around.

var s: Int = 0

def function(s: Int): Boolean={

   var newS = s
   newS = newS + 1 
   s = newS
   return true

}

From What i read you are not passing the same "s" into your function as is in the rest of the code. I am sure there is a even better way but this is working for me.

Ada
  • 97
  • 1
  • 11
0

You don't.

A var is a name that refers to a reference which might be changed. When you call a function, you pass the reference itself, and a new name gets bound to it.

So, to change what reference the name points to, you need a reference to whatever contains the name. If it is an object, that's easy enough. If it is a local variable, then it is not possible.

See also call by reference, though I don't think this question is a true duplicate.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
-1

If you just want to increment a variable starting with 3

val nextId = { var i = 3; () => { i += 1; i } }

then invoke it:

nextId()

Germán Bouzas
  • 1,430
  • 1
  • 13
  • 18
  • This doesn't actually address the question (which was asked, and answered, 4 years ago). – jwvh Nov 22 '17 at 17:41