开发者

Why would you add "or false" to the end of your boolean expression?

开发者 https://www.devze.com 2023-03-20 07:42 出处:网络
I have seen code in lua like this if (a==b or false) then what is the purpose of the \"or false\"? This is the original code:

I have seen code in lua like this

if (a==b or false) then

what is the purpose of the "or false"?

This is the original code:

        Function = function(self, aura, auraID)
        -- Store the version.
        -- aura.Version = 50000;
        -- Fix texmode/glow.
        aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);
      开发者_高级运维  -- Texture source.
        if(aura.owntex or aura.SourceType == PowaAuras.SourceTypes.Icon) then
            aura.SourceType = PowaAuras.SourceTypes.Icon;
        elseif(aura.wowtex or aura.SourceType == PowaAuras.SourceTypes.WoW) then
            aura.SourceType = PowaAuras.SourceTypes.WoW;
        elseif(aura.customtex or aura.SourceType == PowaAuras.SourceTypes.Custom) then
            aura.SourceType = PowaAuras.SourceTypes.Custom;
        elseif(aura.textaura or aura.SourceType == PowaAuras.SourceTypes.Text) then
            aura.SourceType = PowaAuras.SourceTypes.Text;
        else
            aura.SourceType = PowaAuras.SourceTypes.Default;
        end

    end

And here is another example

            -- Iterate over element children.
        visible, hidden = self:UpdateScrollList(key, level+1, visible, hidden, 
            (parentShowing == true and item:GetExpanded() or false));


This looks like a behavior that comes from the syntax:

a=b or false

to initialize a to the value of b or false if b isn't defined.

Within the if statement as you wrote it, I don't see any purpose since the == is evaluated before the or. If they used parenthesis to change the order of operations, then it could be used to validate that b has been defined, e.g.:

> a=nil
> b=nil
> if (a == (b or false)) then print("yikes") else print("aok") end
aok
> if (a == b or false) then print("yikes") else print("aok") end
yikes


It is used to temporarily disable a block of Code.

It's semantics are technically the same as:

#if 0
    dead code goes here ...
#endif


It's to explicitly state the value assigned to the expression when none of its prior conditions are true.

In Lua, the and and or expressions return the values that determine their truth values, rather than the flat truth value itself, like this:

function And(A,B)
  if A then return B --if A is true, then the agreement of both depends on B
  else return A end  --otherwise, they're not, because A wasn't
end

function Or(a,b)
  if A then return A --if A is true, then it doesn't matter if B was true
  else return B end  --otherwise, the truth hinges on B's value
end

In Lua, all values other than false and nil evaluate to true in conditional constructions, so this behavior is commonly used as an idiom to describe default values:

local color = color or 'blue' -- if `color` is defined to any value other than
                              -- `false` (such as any string), it will evaluate
                              -- as true, and the value will be returned from
                              -- the `or` statement (so `color` will be assigned
                              -- its own value and no change will result) -
                              -- if `color` is not defined, the `or` statement
                              -- will evaluate its `nil` value as false and
                              -- return the second value, 'blue'

This idiom can be combined and extended in various ways:

local color = color or self.color
  or (favorites and favorites.color)
  or (type(favourites)=='table' and favourites.colour) 
  or get_defaults('color')
  or {'red','blue','green','yellow'}[math.random(1,4)]

Traditionally, though, they stay fairly simple, with maybe a few chained or cases before the default value.

That's what we're seeing here: the cases where or false is being used are for the last-case value for the expression.

So, while this:

aura.texmode = (aura.texmode == 1 or aura.texmode == true or false);

could have been written as this with exactly the same effect (since the == comparisons will return either true or false):

aura.texmode = (aura.texmode == 1 or aura.texmode == true);

adding the final or false makes it more obvious when looking at that line that the value can be false.

The same goes for the second example, in which the value is being assigned to an argument.


There is no use, because if a~=b then it will try false, which is ... false and return false anyway ...

0

精彩评论

暂无评论...
验证码 换一张
取 消