Incrementing and getting value

前端 未结 3 1450
予麋鹿
予麋鹿 2021-01-13 11:04

Simple scala question. Consider the below.

scala> var mycounter: Int = 0;
mycounter: Int = 0

scala> mycounter += 1

scala> mycounter
res1: Int = 1
         


        
3条回答
  •  一向
    一向 (楼主)
    2021-01-13 11:39

    Sometimes I do this:

    val nextId = { var i = 0; () => { i += 1; i} }
    println(nextId())                               //> 1
    println(nextId())                               //> 2
    

    Might not work for you if you need sometime to access the value without incrementing.

提交回复
热议问题