[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Ternary operator patch
- From: Tomas Guisasola Gorham <tomas@...>
- Date: Thu, 16 Sep 2010 12:10:39 -0300 (BRT)
Imagining we could define no argument anonymous functions like:
local i;
local fn = [i=0; return y];
We could use the choose function like so:
local myvar=choose(expr, [return x], [return y])
Which to some might still look ugly. ("Whats with the line noise?") etc...
I don't like the reuse of `[' and `]' to other purposes,
but the proposal seems interesting. Anyway, both closures will be
created -- no lazy evaluation here.
So how about a syntax to define functions with arguments that are lazily
evaluated?
function choose(expr, [x], [y])
-- x and y are functions that must be called to get their value.
if expr return x() else return y() end
end
I would call this construction 'call-by-lazy-evaluated-value' :-)
If the language already knows that x and y are lazy-expressions, the
programmer should not have to call them explicitly to obtain their values:
function choose(expr, [x], [y])
-- x and y are lazy-expressions; Lua will evaluate them when needed
if expr then return x else return y end
end
Attention: I am NOT proposing the adoption of the above semantics!
usage:
local myvar = choose(i==1, false, true)
I think this is not a good feature since it is not clear to the
caller whether the expressions will be both evaluated or not. Michal
Kottman already provided a better example:
http://permalink.gmane.org/gmane.comp.lang.lua.general/68820
Regards,
Tomás
- References:
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Henk Boom
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Geoff Leyland
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Miles Bader
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Miles Bader
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Jonathan Castello
- Re: Ternary operator patch, Roberto Ierusalimschy
- Re: Ternary operator patch, Alexander Gladysh
- Re: Ternary operator patch, steve donovan
- Re: Ternary operator patch, Jeff Pohlmeyer
- Re: Ternary operator patch, David Kastrup
- Re: Ternary operator patch, Michal Kolodziejczyk
- Re: Ternary operator patch, Michal Kottman
- Re: Ternary operator patch, Eric Man