Menu

#13 Variable path in with-block is evaluated for each statement

Jolie Trunk
pending
thesave
None
5
2015-03-04
2015-01-15
No

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

Discussion

  • Matthias Dieter Wallnöfer

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,6 +1,6 @@
     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 the the expected behavior?
    +Is this the expected behavior?
    
     Test program:
    
    • status: open --> wont-fix
    • assigned_to: Fabrizio Montesi
     
  • Matthias Dieter Wallnöfer

    @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:

    include "string_utils.iol"
    include "console.iol"
    
    main {
            foo.bar[0].a = "a1";
            foo.bar[0].b = "b1";
            foo.bar[#foo.bar].a = "a2";
            foo.bar[#foo.bar].b = "b2";
            valueToPrettyString@StringUtils(foo)(pretty);
            println@Console(pretty)()
    }
    

    I propose you the following solution:

    include "string_utils.iol"
    include "console.iol"
    
    main {
        foo.bar[0].a = "a1";
        foo.bar[0].b = "b1";
            count = #foo.bar;
        with(foo.bar[count]) {
            .a = "a2";
            .b = "b2"
        };
        valueToPrettyString@StringUtils(foo)(pretty);
        println@Console(pretty)()
    }
    
     

    Last edit: Matthias Dieter Wallnöfer 2015-02-28
    • Simon Larsen

      Simon Larsen - 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. :)

       
  • Matthias Dieter Wallnöfer

    @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?

     
  • Matthias Dieter Wallnöfer

    • status: wont-fix --> pending
    • assigned_to: Fabrizio Montesi --> thesave
     
  • thesave

    thesave - 2015-03-04

    Good point.
    Just added an example in the documentation.

     

Log in to post a comment.