Luaの真偽値
Luaでは、falseとnilが偽となり、それ以外の値は全て真です。
function test(x) if x then print("true", x) else print("false", x) end end function main() tab = { -1, 0, 1, "", "string", {}, { 1 }, { test=2 }, main, true, false, } for i = 1, #tab do test(tab[i]) end test(nil) end main()
実行結果です。
true 0 true 1 true true string true table: 0x7fd78b411f10 true table: 0x7fd78b4121c0 true table: 0x7fd78b412200 true function: 0x7fd78b411960 true true false false false nil
数値の0や空文字も真となります。