lua-users home
lua-l archive

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


You probably could create a custom build of Lua. I don't know enough about the code to tell you where though.
 
Or, you could use this:
function toboolean(v)
  if v=='' or v==0 then return false end
  return v and true or false
end  
...
if toboolean(somevalue) then
  --will not run if somevalue is 0 or ''
end
On Mon, Aug 16, 2010 at 10:45 AM, Peyman <peiman_3009@yahoo.com> wrote:

thanks this function works good but i dont want change 0 and null string to true value, there is noway to fix it in Lua, so 0 and null string have false value by default ?

--- On Mon, 8/16/10, Peter Cawley <lua@corsix.org> wrote:

From: Peter Cawley <lua@corsix.org>
Subject: Re: _expression_ statements
To: "Lua list" <lua@bazar2.conectiva.com.br>
Date: Monday, August 16, 2010, 11:14 AM


On Mon, Aug 16, 2010 at 4:36 PM, Peyman <peiman_3009@yahoo.com> wrote:
Hi All,
From Lua manual :
"in particular, the number 0 and the empty string are also true"
so my question is there anyway to create lua that 0 an empty string become true ? is there any work or patch for lua source ? in Lua 5.2 this is possible ?

thanks,
Peyman.


I don't know if it is what you're asking, but:

function toboolean(x) return not not x end
print(toboolean(0)) --> true
print(toboolean("")) --> true
print(toboolean(nil)) --> false