For some time now hansl has had three string-handling commands that
are C-like in their syntax, namely printf, sprintf and sscanf. As we
move towards gretl 2.0 (not immediately imminent; I expect at least
a couple more 1.9.N releases first), I'd like to rationalize these.
At present printf and sprintf are available in command form only,
while sscanf has been both a command and a function for over two
years, with the command being deprecated in favour of the function
since gretl 1.9.4 (2011-02-24). In today's CVS I finally removed the
sscanf command so that only the function remains.
First I'll say what I'd like to do, then I'll offer a rationale.
Proposal: Make both print and sprintf into functions, deprecate the
commands of the same name, and eventually (by 2.0) remove the
commands.
Reasoning: Considered as gretl commands, printf and sprintf are
anomalous, they don't have a structure similar to other commands.
Considered as functions, however, they are not anomalous. And if
they were implemented as functions they would be more efficient (run
faster) and we could remove some ad hoc code that's required to
support them as commands.
Suppose we did this. From the script-writer's point of view printf
would be very little changed. Instead of
printf "x = %g\n", x
you'd write
printf("x = %g\n", x)
(putting in the parentheses; wearing your seat-belt!).
What about sprintf? In this case I think we could usefully change
the structure a little. I would favour
string s = sprintf(format, args)
where the function returns the string created by the substitution of
the arguments ("args") into the format. Since this departs from the
signature of the C function of the same name one could make a case
for renaming the function in hansl ("strbuild" or some such), though
I'm not particularly recommending that.
Related: if printf were to become a function in hansl, what if
anything should it return? The C function returns the number of
characters printed. We could probably arrange that if anyone thinks
it's useful, otherwise it could return 0 on success, non-zero if
anything went wrong, or it could be a "void" function that doesn't
give a return value.
Allin Cottrell