lua-users home
lua-l archive

assert()

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


I've been reading through the "How to Program in Lua Draft" (mentioned on
the list a few days ago) and I noticed the suggested use of "assert()" as a
guard against unexpected errors in functions that return 'nil' to signal an
error has occured. So:
 assert(readline("test"))
Or even:
 x = assert( f() )
But what if the function normally returns multiple values? According to the
Lua5(beta) manual "assert" is equivalent to:
 function assert (v, m)
 if not v then
 error(m or "assertion failed!")
 end
 return v
 end
so:
 a,b,c = assert( f() )
would be doomed to failure (as 'b' and 'c' would never be assigned) :-(.
Would it not make more sense to define "assert()" as:
 function assert(v, ...)
 if not v then
 error(arg[1] or "assertion failed!")
 end
 return v, unpack(arg)
 end
*cheers*
Peter Hill.

AltStyle によって変換されたページ (->オリジナル) /