[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: wrapping functions
- From: Edgar Toernig <froese@...>
- Date: Fri, 04 May 2001 23:01:20 +0200
Jean-Claude Wippler wrote:
>
> > using "(exp)" as a way to force "exp" to have exactly one value,
> > so that {(f(x))} will always create a list with exactly one element.
>
> The feature would definitely be most welcome (it's what I tried first
> when I wanted this capability). But there is a slight visual
> inconsistency: "x=f(g())" vs. "x=(g())" when g returns multiple values.
Hmm... in "x=(g())" the outer () are even superfluous. "x" always gets
only the first results.
> In the first they all go through, in the latter only the first does. So
> overloading () for function arg passing and for single-value extraction
> could be somewhat confusing.
Lua makes a distinction between two kind of parenthesis:
1) function call: <expr> := <expr> '(' <expressionlist> ')'
2) grouping: <expr> := '(' <expr> ')
In your example "x=f(g())" only uses case 1, "x=(g())" uses 1 for g
and 2 for the outer '()'.
Case 2 should get the additional side effect of always giving exactly
one value (notice that case 2 does not allow an expression list, only
an expression with multiple results). This reduction is normally
performed automatically when the compiler detects that multiple
values are not possible. The new side effect allows you to make
it explicit.
> How about the following (gleaned from the Icon language):
> - in "x=(...)", comma's are passed through when the context allows it
Hmm... don't know what you mean with "comma's are passed through". It's
the shortcut for "x=-1(...)" which gives the value of the last expression.
Afaik Icon does not even support multiple return values so in f(g()) f
always gets one argument.
> - that can then be generalized to "x=<N>(...)"
Afaik the general form was "[<expr>] '(' <exprlist> ')'". When expr
is a function it's called with exprlist as it's arguments and if expr
is an integer it gives the nth value from exprlist. If expr is omitted
a default of -1 is used. It's a nice generalization of the parenthesis.
"a=2*(3+4)" is a shortcut for "a=2*-1(3+4)" so the "grouping" '()' are
in fact the same as the function call '()' with the addition that
"a=2*(f(),g(),3+4)" becomes valid too.
[Never noticed this nice generalization before ;-)]
> I have no idea whether this fits into the language. It basically says
> that numbers can act as extraction/projection functions.
Using numbers is no problem and already possible. Just set the call
tag method for the number type to "function(i,...) return arg[i] end"
(maybe some code to handle negative i's). Current Lua does not allow
to write the numbers directly ("a=1(3,4)" gives a syntax error) but
you can use integer variables ("i=1 a=i(3,4)). If I got it right
Lua 4.1 will allow any expression including numbers.
What does _not_ work is omitting the number. Then you get the grouping
'()' which do not allow multiple expressions separated by ','. And
even if one extends/generalizes the syntax to support it, the default
for that missing number would most likely be 1 instead of -1.
It's tempting to have this generalization. If I have some time I'll
look over the parser whether this (allow expression lists in '()')
makes it simpler. On the other side, do we want
if x==(3,f(),g()) then ... end -- call f, call g, check x for 3.
be valid?
Ciao, ET.
PS: The time I played with Icon is long ago. I had to look up some
things while writing this message and please excuse if I got some-
thing wrong...