lua-users home
lua-l archive

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


The following is a non-ideal example, but the idea is that if one adopts the
convention that libraries should return errors rather than throwing
exceptions (see http://lua-users.org/wiki/FinalizedExceptions), then it
would help to have a way to express errors when false and nil are valid
results.

function itemExists( db, itemKey )
    -- Returns true if database db contains an item with the
    -- given key. Returns false if the database does not contain
    -- an item with given key. Returns error in the event of an
    -- error reading the database.

Example usage:

exists = error_assert( itemExists( db, itemKey ) )

Where error_assert is defined like assert except that it only throws if the
first value is the magic error type.

This could almost be layered onto Lua without core changes, but it would be
good if the error value were yet another false value for conditionals.

The nice thing about the approach outlined in FinalizedExceptions is that it
means that the only exceptions one sees are the ones your own code throws.

Mark

on 12/20/04 4:40 PM, Asko Kauppi at asko.kauppi@sci.fi wrote:

> 
> is there really need for this?  give an example..?
> 
> 20.12.2004 kello 19:29, Mark Hamburg kirjoitti:
> 
> Has any thought been given to introducing a dedicated error value/type
> for
>> Lua? nil also means not present in a table or no value for a variable.
>> false
>> is a perfectly legitimate boolean value. The idea would be to have a
>> single
>> value like nil and false that would be intended specifically for the
>> job of
>> saying "an error occurred". One could also make this a type -- e.g.,
>> recognizing error strings as a particular type -- but that probably
>> opens up
>> more cases where one would want to do things like store the value in a
>> table...
>> 
>> Mark
>> 
>