[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Noob question -- On error, exit function and terminate script
- From: Xavier Wang <weasley.wx@...>
- Date: Sun, 26 Jun 2011 00:58:13 +0800
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.
- References:
- Noob question -- On error, exit function and terminate script, marbux
- Re: Noob question -- On error, exit function and terminate script, Rebel Neurofog
- Re: Noob question -- On error, exit function and terminate script, Xavier Wang
- Re: Noob question -- On error, exit function and terminate script, Timothy Hunter
- Re: Noob question -- On error, exit function and terminate script, Xavier Wang
- Re: Noob question -- On error, exit function and terminate script, Lorenzo Donati