[Gretl-devel] members of lists returned from functions

Allin Cottrell cottrell at wfu.edu
Mon Feb 11 14:35:00 EST 2008


On Sun, 10 Feb 2008, Sven Schreiber wrote:

> Am 10.02.2008 17:04, Allin Cottrell schrieb:

> > 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? ...

[ and went on to suggest that in this case the original values
of the variables in question should be overwritten ].

> 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. 

Yes, I agree that's a possible issue.  

I was going to say there's a precedent for this sort of thing with 
various built-in commands/functions -- such as logs, lags, and 
diff -- which generate automatically-named variables.  However, I 
notice that at some point we adopted the policy of avoiding name 
collision by mangling the names of the generated series if 
necessary.

<script>
open data4-10
series l_ENROLL = 10
logs ENROLL
print l_EN*
</script>

After this we have l_ENROLL, which still equals 10, and l_ENROLa,
which holds the log of ENROLL.

We could try something similar with list-returning user functions, 
but actually on second thoughts I'm not so sure this is the right 
thing to do.  I'm inclined to say that so long as the names used 
by the function are clearly stated in its documentation -- so the 
user can take evasive action if necessary -- the cleaner policy is 
to overwrite.

I was a bit surprised by the behavior of "logs" noted above, and 
I'm not sure it really conforms to intent; as I recall, the 
primary intent behind name-mangling was to ensure uniqueness 
in case the names of the source variables were too long.  E.g.

logs longnameseries1 longnameseries2

where something has to be done or we'll get 2 variables named
l_longnameserie.

For now I have it that list-returning user functions just 
overwrite, but perhaps we need to discuss this some more.

Allin.









More information about the Gretl-devel mailing list