サーチ…
備考
ブール、真実、および虚偽はルアでは簡単です。レビューする:
- :正確に二つの値を持つboolean型があり
trueとfalse。 - 条件付きコンテキスト(
if、elseif、while、until)では、ブール値は必要ありません。任意の式を使用できます。 - 条件文脈では、
falseとnilはfalseとしてカウントされ、それ以外はすべてtrueとカウントされます。 - 3は既にこれを意味していますが、他の言語から来ている場合は、Luaの条件文脈で
0と空文字列が真であることを覚えておいてください。
ブール型
ブール値などの値
luaを扱うときは、ブール値trueとfalse 、および真または偽と評価される値を区別することが重要です。
luaにはfalseと評価される2つの値しかありません: nilとfalse 、数値0を含む他のすべてがtrueに評価されます。
これが意味することのいくつかの例:
if 0 then print("0 is true") end --> this will print "true"
if (2 == 3) then print("true") else print("false") end --> this prints "false"
if (2 == 3) == false then print("true") end --> this prints "true"
if (2 == 3) == nil then else print("false") end
--> prints false, because even if nil and false both evaluate to false,
--> they are still different things.
論理演算
luaの論理演算子は必ずしもブール値を返すわけではありません:
and最初の値がtrueと評価された場合に第2の値を返します。
or最初の値が偽と評価された場合に第2の値を返します。
これにより、他の言語と同様に、三項演算子をシミュレートすることができます。
local var = false and 20 or 30 --> returns 30
local var = true and 20 or 30 --> returns 20
-- in C: false ? 20 : 30
これは、存在しない場合にテーブルを初期化するためにも使用できます
tab = tab or {} -- if tab already exists, nothing happens
またはif文の使用を避けるために、コードを読みやすくする
print(debug and "there has been an error") -- prints "false" line if debug is false
debug and print("there has been an error") -- does nothing if debug is false
-- as you can see, the second way is preferable, because it does not output
-- anything if the condition is not met, but it is still possible.
-- also, note that the second expression returns false if debug is false,
-- and whatever print() returns if debug is true (in this case, print returns nil)
変数が定義されているかどうかの確認
存在しない変数はnil返すので、変数が存在するかどうか(定義されている場合)を簡単にチェックすることができます。これはfalseと評価されます。
local tab_1, tab_2 = {}
if tab_1 then print("table 1 exists") end --> prints "table 1 exists"
if tab_2 then print("table 2 exists") end --> prints nothing
これが当てはまらない唯一のケースは、変数が値false格納する場合です。この場合、技術的には存在しますが、それでもfalseに評価されます。このため、状態や入力に応じてfalseとnilを返す関数を作成するのは悪い設計です。しかし、私たちがnilか否かを確認することはできfalse :
if nil == nil then print("A nil is present") else print("A nil is not present") end
if false == nil then print("A nil is present") else print("A nil is not present") end
-- The output of these calls are:
-- A nil is present!
-- A nil is not present
条件付きのコンテキスト
Luaの条件付きコンテキスト( if 、 elseif 、 while 、 until )はブール値を必要としません。多くの言語と同様に、任意のLua値は条件に現れることがあります。評価のルールは簡単です:
falseとnilはfalseとしてカウントされます。他のすべては真実とみなされます。
if 1 then print("Numbers work.") end if 0 then print("Even 0 is true") end if "strings work" then print("Strings work.") end if "" then print("Even the empty string is true.") end
論理演算子
Luaでは、ブール値は論理演算子で操作できます。これらの演算子には、 not 、 and 、 orます。
簡単な表現では、結果はかなり単純です。
print(not true) --> false
print(not false) --> true
print(true or false) --> true
print(false and true) --> false
優先順位
優先順位は、演算演算子unary - 、 * 、 +と同様です。
-
not - その後
and - その後、
or
これは複雑な表現につながります:
print(true and false or not false and not true)
print( (true and false) or ((not false) and (not true)) )
--> these are equivalent, and both evaluate to false
ショートカット評価
演算子andおよびorは、第1オペランドを使用して評価されるorがあります(ただし、第2オペランドは不要です)。
function a()
print("a() was called")
return true
end
function b()
print("b() was called")
return false
end
print(a() or b())
--> a() was called
--> true
-- nothing else
print(b() and a())
--> b() was called
--> false
-- nothing else
print(a() and b())
--> a() was called
--> b() was called
--> false
慣用の条件演算子
論理演算子の優先順位、ショートカット評価の可能性、および非falseおよび非nil値の評価がtrueであるため、慣用条件付き演算子はLuaで利用できます。
function a()
print("a() was called")
return false
end
function b()
print("b() was called")
return true
end
function c()
print("c() was called")
return 7
end
print(a() and b() or c())
--> a() was called
--> c() was called
--> 7
print(b() and c() or a())
--> b() was called
--> c() was called
--> 7
また、 x and a or b構造の性質上、 aがfalseと評価された場合、 aは返されません。この条件は、 xが何であっても常にbを返します。
print(true and false or 1) -- outputs 1
真理値表
Luaの論理演算子はブール値を返すのではなく、その引数の1つを返します。 falseの場合はnilを、trueの場合は数値を使用して、それらの動作を以下に示します。
print(nil and nil) -- nil
print(nil and 2) -- nil
print(1 and nil) -- nil
print(1 and 2) -- 2
print(nil or nil) -- nil
print(nil or 2) -- 2
print(1 or nil) -- 1
print(1 or 2) -- 1
ご覧のように、Luaは常にチェックを失敗または成功させる最初の値を返します。ここにそれを示す真理値表があります。
x | y || and x | y || or
------------------ ------------------
false|false|| x false|false|| y
false|true || x false|true || y
true |false|| y true |false|| x
true |true || y true |true || x
それを必要とする人には、これらの論理演算子を表す2つの関数があります。
function exampleAnd(value1, value2)
if value1 then
return value2
end
return value1
end
function exampleOr(value1, value2)
if value1 then
return value1
end
return value2
end
'および' 'または'論理演算子を使用して三項演算子をエミュレートする。
ルアでは、論理演算子andおよびorは、ブール結果の代わりに結果としてオペランドの1つを返します。結果として、このメカニズムは、言語の中で '本当の'三項演算子を持たないluaにもかかわらず、三項演算子の動作をエミュレートするために利用することができます。
構文
条件 と truthy_expr または falsey_expr
変数の代入/初期化で使用する
local drink = (fruit == "apple") and "apple juice" or "water"
テーブルコンストラクタで使用する
local menu =
{
meal = vegan and "carrot" or "steak",
drink = vegan and "tea" or "chicken soup"
}
関数の引数として使用する
print(age > 18 and "beer" or "fruit punch")
return文での使用
function get_gradestring(student)
return student.grade > 60 and "pass" or "fail"
end
警告
このメカニズムが望ましい動作をしない状況があります。この場合を考慮する
local var = true and false or "should not happen"
'本当の'三項演算子では、 varの期待値はfalseです。ただし、2番目のオペランドが偽であるため、luaでは評価and評価が「失敗」します。その結果、 varは代わりにshould not happenでshould not happen 。
この問題に対する2つの可能な回避策は、中間のオペランドが偽でないようにこの式をリファクタリングします。例えば。
local var = not true and "should not happen" or false
あるいは、古典的なif then else構造を使用しelse 。