|
![](/i/fill.gif) |
Warp wrote:
> Tor Olav Kristensen <tor### [at] toberemovedgmail com> wrote:
>>> #macro TEST(A)
>>> #local A = A + 1;
>>> #end
>>>
>>> #declare B = 1;
>>>
>>> TEST(B)
>> ...
>
>> Your code above may work, but was it meant to work that way ?
>> I.e. Is the behaviour above a bug - or is it a feature ?
>> :)
>
>> To me it is illogical that a #local statement should be
>> able to change _anything_ outside its scope.
>
> OTOH, how should it behave?
>
> Macro parameters are passed "by reference". This means that you can
> do this:
>
> #macro TEST(A)
> #declare A = A+1;
> #end
>
> and it will modify the original identifier given as parameter.
>
> You want #local to not to modify the original identifier. So basically
> you want #local to make a local copy of the identifier and modify only that.
> But then all kinds of problematic situations may arise, for instance:
>
> #macro TEST(A)
> #local A = A+1;
> #declare A = A+1;
> #end
>
> Should the #declare modify the local copy or the original identifier?
>
> More importantly: There are two A's now, the original and the local copy.
> Which 'A' should be used in the right-hand-side of the '=' of the #declare,
> the local copy or the original variable? If the #local only modifies a copy,
> then this "local A" will have a different value than the original identifier.
I see the problem.
(So if this is meant as a feature, it would be nice if the
documentation describes more in detail what effect #local has
on macro parameters.)
Maybe a solution could be to have only one assignment
directive (e.g. #set or #let or none) and let call-by-value
be the default for parameters to macros. call-by-reference
could be indicated by e.g. "var" in the macro declaration.
#macro TEST(A, var B)
#set A = A + 1;
#set B = B + A;
#end
#set C = 1;
#set D = 3;
TEST(C, D)
// C is still 1. D has been changed to 5.
Another possibility could be to use "in", "out" and "in out"
in front of the parameters, like in Ada.
http://www.csc.liv.ac.uk/~grant/Teaching/COMP205/paramPassing.html
--
Tor Olav
http://subcube.com
Post a reply to this message
|
![](/i/fill.gif) |