On Fri, 19 Jun 2015, Allin Cottrell wrote:
On Fri, 19 Jun 2015, Sven Schreiber wrote:
> instead of creating an interim list variable I wanted to use the
> returned list from a function directly for further operations, but that
> failed:
>
> <hansl>
> function list checkl(void)
> genr time
> list out = const time
> return out
> end function
>
> matrix v = zeros(2,1)
>
> list foo = checkl()
> series hey1 = lincomb(foo, v) # works
> print hey1
>
> series hey2 = lincomb(checkl(), v) # doesn't work
> print hey2
> </hansl>
>
> I would think that's a bug.
I agree. On examining the relevant code (the function eval_ufunc(), in the
source file geneval.c) I found an if-condition with a comment dated
2015-02-02 which reads:
"collect" the return value [from a user-defined function that returns a list]
only on direct assignment
It's easy enough to remove that condition (which then makes your example
work), but I need to try to remember what prompted me to insert it in the
first place -- I presume it was in response to some other bug. (Note to self:
write more explicit comments!)
OK, I've reconstructed the rationale that I had in mind back in
February. This is a tricky issue. Consider Sven's function once again:
function list checkl (void)
genr time
list out = const time
return out
end function
Now I call checkl() without assigning the return value to anything:
<hansl>
nulldata 10
matrix v = {2,1}
series hey2 = lincomb(checkl(), v)
varlist
</hansl>
Should the "time" variable, which is added to the dataset and included
in the "out" list returned by checkl(), be present in the "main"
dataset after the function call?
I'd be inclined to say No, but that is the consequence of "collecting"
or "accepting" the returned list, even though it's not assigned to
anything. A list is a set of references to series, and it's broken if
those series don't exist. The lincomb() call can work only if "time"
is a member of the caller's dataset -- qua member of an anonymous list
-- but then the two function calls have the potentially problematic
side-effect of adding a series to the caller's dataset without any
explicit authorization.
I don't see any easy way around this, but I'm open to suggestions.
Allin