On Sat, 26 Jul 2008, Gordon Hughes wrote:
Yes, I had worked out the mechanism that you describe for
providing optional arguments. However, it does not work for all
argument types. Also, the order of the arguments is important.
To explain:
A. The current version (1.7.6rc1) won't accept the following
syntax in an argument list
function xxx( ... string *var[null], ... )
which means that string arguments cannot be omitted. As far as
I can work out, it is valid to use this syntax for integers,
scalars, matrices and series, but not for lists and strings.
It's fine to use "null" for lists. If a list argument is given as
"null", or a trailing list argument with a null default is
omitted, the function gets an empty list. This is in the User's
Guide.
As for string arguments, the lack of a facility for setting a null
default was an oversight, now fixed in CVS/snapshot. The syntax
is now the same as for lists, e.g.,
function foo (series y, string s[null])
(Note, the pointer mechanism " *" is not supported.)
Two little sample scripts follow. The first already works with
yesterday's build; the second works with today's.
<script1>
function test (series y, list L[null])
print y
if nelem(L) > 0
print L
else
print "L is null"
endif
end function
open data4-1
test(price)
test(price, null)
list X = 2 3
test(price, X)
</script1>
<script2>
function test (series y, string s[null])
print y
if isnull(s)
print "s is null"
else
printf "s = %s\n", s
endif
end function
open data4-1
test(price)
test(price, null)
string s = "hello"
test(price, s)
</script2>
B. The argument list is read from left to right - fair enough
but it means that if a user provides 3 arguments for a function
with, say, 5 potential arguments, then these 3 arguments are
mapped to argument 1, argument 2, argument 3. This is entirely
reasonable for the programmer but less obvious for the novice
user who wants to define arguments 1, 2 & 5.
I tend to think it's up to the function-writer to assess which
optional stuff is most likely to be wanted, and push the more
arcane items to the end of the argument list. I agree this won't
suit "all of the people, all of the time".
That is really my point about the convenience of using
$accessors rather than pointer arguments.
I'm not very happy with the idea of $ accessors for objects
created during a function call. When a function exits, one wants
to clean up, freeing all the temporary resources it has used. I
wouldn't like to have functions "leaving a bunch of junk around"
after they exit.
As a long-term idea, Jack's suggestion of allowing functions to
traffic in "structures" (ad hoc collections of data of various
sorts) seems a nice one, and might help in this context.
Allin.