Variable path in with-block is evaluated for each statement
A service-oriented programming language.
Brought to you by:
fmontesi
The variable path in a with-block seems to be evaluated for each statement in the block. This means if the variable path is a vector and it's size is used, the path changes. See example below.
Is this the expected behavior?
Test program:
include "string_utils.iol"
include "console.iol"
main {
foo.bar[0].a = "a1";
foo.bar[0].b = "b1";
with(foo.bar[#foo.bar]) {
.a = "a2";
.b = "b2"
};
valueToPrettyString@StringUtils(foo)(pretty);
println@Console(pretty)()
}
Output:
Value
.bar[0]
.b[0] = b1 : java.lang.String
.a[0] = a1 : java.lang.String
.bar[1]
.a[0] = a2 : java.lang.String
.bar[2]
.b[0] = b2 : java.lang.String
What I would expect:
Value
.bar[0]
.b[0] = b1 : java.lang.String
.a[0] = a1 : java.lang.String
.bar[1]
.a[0] = a2 : java.lang.String
.b[0] = b2 : java.lang.String
Diff:
@Fabrizio: please tell me if I am wrong
@Simon: I think this is no bug. "with()" is really only a path shortcut instruction. Please consider this semantically equivalent program, to understand why you create a third array member:
I propose you the following solution:
Last edit: Matthias Dieter Wallnöfer 2015-02-28
I am well aware of why it happens. I just found it unintuitive and was curious as to whether it was intentional or just a product of the current implementation. If you think of it in terms of variable paths I suppose it makes more sense.
Regardless I think it might be worthwhile to note this in the documentation to spare others the headache. :)
@Simon, I guess you are right, that with() could be documented better.
@Saverio, could you please help us to add such an example about with() to the documentation?
Good point.
Just added an example in the documentation.