how do I increment an integer variable I passed into a function in Scala?

后端 未结 5 1417
深忆病人
深忆病人 2021-01-18 05:34

I declared a variable outside the function like this:

var s: Int = 0

passed it such as this:

def function(s: Int):         


        
5条回答
  •  别那么骄傲
    2021-01-18 06:03

    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
    

提交回复
热议问题