Skip to content

$-variables capture #835

@veretin

Description

@veretin

Hi!

Suppose, we want to expand each A(q?) as B(q)+C(q) if there is tag(q).
Here is the code with $ variables.

c A,B,C,tag,tag2;
v q1,q2,q3,k;

g ff =
       + A(q1)*A(q2)*A(q3) * tag(q2)*tag(q3);

#do i=1,2
*  repeat;                                                                                                                              
    if ( match( tag(k?$k) ) );
      id tag($k) = tag2($k);
      id,many, A($k) = B($k) + C($k);
    endif;
*  endrepeat;                                                                                                                           
*.sort                                                                                                                                  
#enddo

id tag2(?a) = tag(?a);
moduleoption local $k;
.sort

b tag,tag2,A;
print+s ff;
.end

This gives

      + A(q1)*tag(q2)*tag(q3) * (
          + B(q2)*B(q3)
          + B(q2)*C(q3)
          + B(q3)*C(q3)
          + C(q3)^2
          );

which I find wrong.
But if I uncomment .sort then it works:

 + A(q1)*tag(q2)*tag(q3) * (
          + B(q2)*B(q3)
          + B(q2)*C(q3)
          + B(q3)*C(q2)
          + C(q2)*C(q3)
          );

With repeat, instead of #do-cycle, the same story.

In this particular case we can work without $-variable

id tag(q?)*A(q?) = tag(q)*(B(q) + C(q));

But in real code, I need some further manipulations with q.

Metadata

Metadata

Assignees

No one assigned

    Labels

    expected-behaviourIssues which are correct behaviour, despite perhaps being a bit surprising.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions