[Omake] Function that modifies the environment and returns a value

Aleksey Nogin anogin at hrl.com
Wed Nov 28 09:39:48 PST 2007


On 28.11.2007 08:54, Alain Frisch wrote:

> It seems that a function can modify the environment of its caller, as 
> demonstrated by the following script which prints "a":
> 
> FILES=
> f(file) =
>   FILES += $(file)
>   export FILES
> f(a)
> echo $(FILES)
> 
> If I add a return statement before or after the export statement, the 
> update on FILES is not visible outside the function.
> 
> Is it possible to write a function which modifies the environment of its 
> caller and returns a value at the same time?

Alain,

The "return" is essentially an "abort" instruction which can be used 
anywhere inside the function body, so the function does not get to 
export if it's "aborted" with a return.

However, if the function is executed without ever being aborted by the 
explicit "return" statement, then it will return the value produced by 
its last statement. Starting with version 0.9.8.5, the "export" does not 
have to be the last line in a block, so in this case the function may 
both return and export.

E.g.

FILES=
f(file) =
   FILES += $(file)
   export FILES
   value 5
x = $(f a)
println($(x)-$(FILES))

prints "5-a".

Aleksey

-- 
Aleksey Nogin, Research Scientist
Advanced Technologies Department, Information & System Sciences Lab
HRL Laboratories, LLC, Malibu, CA


More information about the Omake mailing list