Unfortunately you can’t. Its a limitation of the language unfortunately. When I’ve encountered this sort of problem I’ve either found a way to not need the reference to the local variable or resorted to using a while loop, regular for loop, or Iterator.
That should work fine and I should elaborate on my original answer. You can access an outside variable in the foreach but only to read it. You can’t modify it.
An even easier approach (equally as ugly) would be to declare a hashMap or arryList using “val” with your variable in it. The container would be final so the foreach can get to it but you can still modify the stuff inside.
Indeed but in my experience, since I set up persistence nearly a year ago I have never seen an Undefined state in any of my rules. It is pretty rock solid in my experience, reliable enough that I have no checks for it in any of my rules.