On Wed, 23 Jul 2008, Sven Schreiber wrote:
Sorry for not participating earlier in this important
discussion, I'm currently semi-offline.
I have a couple of questions about the proposed solution:
1. Just thinking out loud, not really proposing this, but before
the syntax listname.varname is fixed for the next 10 years or so
what about different styles like listname[varname] or whatever
else may make sense? (Compare the syntax with gretl's way of
accessing individual observations in a variable.)
Fair point, but one nice thing about '.' is precisely that it
won't get confused with identifier[...] for observations from
series.
Jack originally proposed listname->varname, which is C-style
syntax that's currently unused in gretl. I tend to favour '.'
because it's one less character to type, and because we already
have the syntax of object.member for some uses (e.g.
model.$coeff).
2. What about indexing the list members not by name but by
number? For example in a foreach loop it would be slightly more
intuitive to just iterate over the i=1...n instead of getting
the names via $i. (I'm aware this is not a compelling example,
but just to raise the question before it's too late.)
I'm not exactly sure what you mean here. We do iterate over
i=1..n at present; that's what plain "i" retrieves. The question,
I think, is what "$i" should get. (Using "$i" at all is optional,
of course; some loops over lists won't need it.)
3. I'm not sure I understand whether in your solution the outer
scope can be affected or not. First, would xlist.myvar = 0 work
inside a function and would myvar be changed outside the
function? Second, can the structure of xlist itself be changed
inside the function and affect xlilst outside the function?
(Think of adding list members.)
I'm not proposing any change to the current situation in this
respect. The situation is:
* If you pass a list to a function, the member series can be
changed (e.g. using "myvar = 0", where myvar is a variable in the
list, or using "xlist.myvar = 0" in the proposed scheme).
Unless, that is, the list argument is marked as "const", in which
case you'll get an error if you try to mess with a list member.
This is in the User's Guide.
* Given a list provided as a function argument, you can can change
the list within a function (e.g. add/delete members), but this
won't affect the list at the caller level -- the original list is
restored on exit. (We could add the facility of passing a
"pointer to list" such that the list could be changed permanently,
but we don't have that at present. If you want to give the
caller a modified list you have to return a list directly.)
4. If gretl performs a computer-memory copy of the list member
values in order to pass them to a function, what about speed
issues if lots of data are involved? Or to put it differently,
is it possible with the new solution to somehow force avoidance
of the copy?
There's no copying of values of list-member series. The existing
values are simply made accessible. The difference under the
current proposal is just that those values will not be accessible
using the plain name of the variable. (My earlier attempt at
solving this problem involved copying values using a function
called varcopy, but that's off the table now.)
> I'm not averse to starting work on a new branch, to become
> 2.0, with more backward-incompatible changes. What changes
> you [Jack] have in mind?
I'm curious what Jack will propose, but reading your function
example reminded me that I find the syntax with respect to $i
awkward. For example instead of $i3 I would prefer to see
explicit string concatenation like $i + '3' or so. IMHO it would
be easer to read. I remember that I had similar feelings about
the @-macro stuff, @userdir and such.
That's open for discussion, certainly.
> I think the trade-off here favours making 0/false the default.
yes!
Sorry, I got confused and mis-stated the trade-off. Here's a
corrected account; the trade-off is more symmetrical than I
indicated.
* If protect_lists is false by default, people can write functions
that are _potentially_ list-safe (using the listname.varname
syntax, or whatever we settle on), but they won't _actually_ be
safe unless the user does "set protect_lists on". (For safety to
kick in, we have to make list-argument variables invisible by
their plain names, which is what protect_lists does.)
* If protect_lists is true by default, the situation is as I
described before. You can get backward compatibility for
functions that reference list-argument members directly, by
turning protect_lists off.
> I also think that this sort of consideration raises another
> issue: we should add an internal constant, with a $-accessor,
> representing the gretl version, a.s.a.p. (i.e. in gretl
> 1.7.6). Thereafter, function writers will be able to program
> around differences between gretl versions, if they so choose.
That would be good, too. BUt I guess that would also mean that
in cvs/snapshot versions the number would have to be increased
right after release, no?
Or as soon as we introduce a relevant change (backward- or
forward-incompatible) in CVS?
Allin.