In your example there is no use for counter
. So it is hard to guess what your real usecase is. In most cases some calculation based on i
is enought, eliminating counter
completely. Orionll's answer is pointing in that direction.
If however counter
is something which cannot be computed from i
but is some accumulated state, then the simplest thing is to extract that state into a custom class and to modify this state using void
methods. In this example the generated void setCounter(int)
method is used.
class Example {
def static void main(String[] args) {
println(generate)
}
def static generate() '''
«var state = new State(0)»
«FOR i : 0 ..< 10»
Line Numnber: «i»
«state.setCounter(state.counter + 1)»
«ENDFOR»
'''
}
class State {
@Property
var int counter
new(int counter){
this.counter = counter
}
}