[Ferm] adding to a list

Max Kellermann max at duempel.org
Fri Mar 4 09:26:43 CET 2011


On 2011/03/03 22:19, Marc Haber <mh+ferm at zugschlus.de> wrote:
>  5   @def &add_l($ip) = {
>  6     @def $l=($l $ip);
>  7   }
>  8     
>  9   @add_l(192.0.2.20);

There is no built-in function named "@add_l".  To refer to a custom
function, you need "&add_l" (like your second example).

You're right, it doesn't work because of scoping: within the function
call, a new stack frame is created, and all variables defined here are
local.

> 11   @def &add_to_list($list,$ip) = {
> 12     @def $list=($list $ip);
> 13   }
> 14   
> 15   &add_to_list($l,192.0.2.30);

Function parameters are special: they are expanded to their values
before the function is evaluated.  So what this call generates is:

 @def (192.0.2.1 192.0.2.10.) = (192.0.2.1 192.0.2.10. 192.0.2.30);

> Is it possible to do what I want to do?

You cannot modify a "parent stack frame" variable with the first
variant.  This could maybe be implemented with something such as:

 @def @parent_stackframe($l) = ....

(Ugly name, just to demonstrate the point) .. which would tell ferm to
refer to another stack frame.  Not an elegant solution.

Second variant: we could add passing parameter "by reference",
outline:

 @def &add_to_list(@by_ref($list), $item) = {
   @def $list = ($list $item);
 }

"@byref" would tell ferm to substitute $list references by the actual
variable passed here, not by its value.  That seems like the more
elegant solution to me.

Max


More information about the Ferm mailing list