lua-users home
lua-l archive

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




2011/6/26 Lorenzo Donati <lorenzodonatibz@interfree.it>
On 25/06/2011 18.18, Xavier Wang wrote:

[snip]


Sorry, but I mean, a new version of assert against the baselib one that
_don't_ evaluate the second _expression_ if the first _expression_ is true.

I don't think this is possible without altering deeply the semantics of the language, since "assert" is a regular function (although implemented in C), so all its arguments are evaluated before the call.

The possible solutions I see both involve change in the language:

1. turn "assert" into some sort of operator, which its kind of short circuit evaluation (like "and" and "or").

2. introduce a way of telling Lua to delay the evaluation of an _expression_ (this has been proposed before IIRC).

I wouldn't want the first (too specific). Maybe the second could have its applications if done well and it may be general enough to fit Lua's spirit.

P.S.: note that you can achieve sort of "short circuited" assert writing:

local _ = condition or error( msg )

instead of

assert( condition, msg )

But I don't like that idiom too much. It doesn't save too much typing compared to:

if not condition then error( msg ) end

and it is less explicit (i.e. unreadable ).


-- Lorenzo

Some Idea:
may be the last one statement of a block maybe a _expression_, so we can write:
do condition or error( msg ) end

this may better.

I like the idea of lazy evaluation, also.