[Gretl-devel] members of lists returned from functions

Sven Schreiber svetosch at gmx.net
Sun Feb 10 12:04:55 EST 2008


Am 10.02.2008 17:04, Allin Cottrell schrieb:
> On Sat, 9 Feb 2008, Sven Schreiber wrote:
> 
>> The example script below prints two identical series under the 
>> name of "s1", which is "obviously" wrong...
> 
> Ah, there's a loose end here, in terms of behavior when a function 
> returns a list: what should happen if the list contains 
> one or more variables that have the same name as a variable that 
> already exists at the caller level?  
> 
> What's happening at present can be exposed by this variant on 
> Sven's script:
> 
> function test(void)
>   series s1 = randgen(u,0,1)
>   series s2 = randgen(u,2,3)
>   list myl = s1 s2
>   print "in test"
>   print s1 -o
>   return list myl
> end function
> 
> nulldata 4
> loop 2
>   list check = test()
>   varlist
>   print "in caller"
>   print s1 -o
> end loop
> 
> The last "varlist" produces:
> 
> Listing 6 variables:
>   0) const    1) index    2) s1       3) s2       4) s1     
>   5) s2     
> 
> which is not nice.  It seems to me the second iteration should 
> overwrite s1 and s2 -- agreed?
> 

At first I thought "yes" (and it was what I was proposing originally), 
but then it occurred to me that the fact that returning lists from 
functions would affect the caller's namespaces is a "policy breach". 
Because in theory it's possible that at the caller level there exists a 
series of the same name just by coincidence, which would then get 
deleted. So before you could use a function written by somebody else you 
would have to study it and make sure it doesn't have this side effect. 
IMHO that's bad.

Ok, so the probability of this problem occurring in practice may be 
minimal, and so feel free to discard this objection.

But without having thought too much about it, let me just give an 
example of how it could perhaps work:

Suppose that "list indexing" is introduced as a new syntax; that would 
mean that in the above example you could access the series within the 
list 'check' by a one-dimensional index: check[1], check[2] etc. up to 
check[nelem(check)]. (And you would not be able to use the names 's1' or 
's2' at the caller level, they would be restricted to the 
scope/namespace of the function.) However, I admit it's not 100% clear 
how the series would be displayed in the GUI without running into the 
namespace collision problem again.

just a thought,
sven


More information about the Gretl-devel mailing list