lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

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
	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

	Attention: I am NOT proposing the adoption of the above semantics!

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: