lua-users home
lua-l archive

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

Lots of thanks Javier!, just opted for second option, and will include it for next release.


2010/1/23 Javier Guerra <>
Fernando P. García wrote:
> Hello,
> I'm pleased to announce that Nutria Seawolf is ready to your feedback.
> Nutria 0.6 is a PHP Standard Library, why not give it a try if you ever
> wanted to do your webscripts in your favorite programming language?

nice effort (even if i question the point of replicating PHP's vices).  I wanted to check the 'feel' of the code, and i don't know why, but i zeroed on the empty() implementation:

function empty(var)
       (var == nil) or
       (type(var) == 'boolean' and var == false or false) or
       (type(var) == 'number' and var == 0 or false) or
       (type(var) == 'string' and (var == '' or var == '0') or false) or
       (type(var) == 'table' and next(var) == nil or false)

remember that in Lua, two values can only be equal if they're of the same type, so you don't have to check the parameter's type before comparing:

function empty(var)
       var == nil or
       var == false or
       var == 0 or
       var == '' or
       var == '0' or
       (type(var)=='table' and next(var) == nil)

also, when you compare a single value to several options, another option is to use a local table:

   local falses = {
       [false] = true,
       [0] = true,
       [''] = true,
       ['0'] = true,
   function empty (var)
       return not var or falses[var] or (type(var) == 'table' and next(var)==nil)


Fernando P. García,
Developer - Analista de Sistemas
+51 1 9 8991 7871, Calle Santa Catalina Ancha #377, Cusco -Perú

** Antes de imprimir este mensaje piensa en tu compromiso con el medio ambiente, protegerlo depende de tí.