Daniel Fagerstrom wrote:
Ok, that explains the behaviour, the question is what we should do about the, admitingly, weird behaviour.
How it is weird - it's same as in xslt.
Not exactly, while xsl:variable is like a final declaration in Java, jx:set is an asignment and can be used for e.g. counting things in a forEach. The "weird" part is that you can't define a global variable and use it as a counter in a sequence of loops as it will be redifined in the current context if it does not exist there. See Eric Burghard's example earlier in this thread.
One possibilty would be to decide that we want JXTG to be more like a functional language.
+1
In that case we should deprecate jx:set and introduce a jx:let instead that just gives a local name for an expression and that gives an exception if you try to set the "variable" to a new value.
Not sure this is necessary.
I think I would prefer this behaviour as I would prefer having a template language without side effects.
+100
Another possibility is to let set asign the value to the first variable binding with the same name that it finds when the stack is searched,
-1. Don't think this is a possibility; as noted above, templates should be side effect free.
Glad that you agree :) Eric and Leszek are not convinced at all and want real assignment.
I would also prefer complete freeness of side effects, but that would require us to write an own expression language as both Jexl and JXPath allows for calling put methods etc.
There is the same problem with extension functions in XSLT, but of some reason people seem much more interested in using side effects in JXTG than in XSLT. Maybe JXTG lacks some functionallity needed for important use cases or maybe it is still another instance of the hammer anti pattern ;)
/Daniel
