[英]“|=” operator caches static field value?
I was having an interesting bug with changes not being registered.我遇到了一个有趣的错误,未注册更改。 The fix was unusual.
修复是不寻常的。 The first code works fine, while the second one - doesn't.
第一个代码工作正常,而第二个代码 - 没有。
https://rextester.com/PIRZFJ12810 https://rextester.com/PIRZFJ12810
globChanged = false;
bool tmp = target.Inspect();
globChanged |= tmp;
if (globChanged) Debug.LogError("Works!");
This one doesn't work:这个不行:
globChanged = false;
globChanged |= target.Inspect();
if (globChanged) Debug.LogError("Works!");
I think I have a suspicion.我想我有一个怀疑。
globChanged
is a public static field. globChanged
是一个公共 static 字段。 The target.Inspect();
target.Inspect();
actually returns False.实际上返回 False。 But internally it sets
globChanged = true
when changes are made.但在内部,它会在进行更改时设置
globChanged = true
。 I think the operator "|=" evaluates the value of globChanged
before the Inspect();
我认为运算符 "|=" 在
Inspect();
之前评估globChanged
的值; function was called.调用了 function。 And I actually tested this - at the end of
target.Inspect();
我实际上对此进行了测试 - 在
target.Inspect();
结束时the globChanged
is True. globChanged
为真。
Also: Doesn't work:另外:不起作用:
globChanged = globChanged || pgi.Inspect();
Works:作品:
globChanged = pgi.Inspect() || globChanged;
To clarify.澄清。 Doesn't work means that It returns False when globChanged was set True.
不起作用意味着当 globChanged 设置为 True 时它返回 False。 pgi.Inspect();
pgi.Inspect(); is executed every time and globChanged is False at the start.
每次都执行,并且 globChanged 在开始时为 False。
The expression value |= DoSomething
is nothing but this:表达式
value |= DoSomething
只不过是这样:
_value = _value | DoSomething
In your first example you set tmp
to whatever Inspect
returns - false
.在您的第一个示例中,您将
tmp
设置为Inspect
返回的任何内容 - false
。 However that method also sets globalChange
to true
.但是,该方法还将
globalChange
设置为true
。 Afterwards globalChange
is evaluated in the next line.然后
globalChange
在下一行进行评估。 It is now true
, which results in true | false
现在是
true
,结果为true | false
true | false
. true | false
的。
The second example is a bit more tricky.第二个例子有点棘手。
The |
该| operator evaluates both operands even if the left-hand operand evaluates to true, so that the operation result is true regardless of the value of the right-hand operand.
即使左侧操作数的计算结果为真,运算符也会计算两个操作数,因此无论右侧操作数的值如何,运算结果都为真。
As mentioned here both expressions are evaluated from the left to the right .正如这里提到的,这两个表达式都是从左到右计算的。 So first
_gloablChange
is evaluated, which is false
, as of its initial value.所以首先
_gloablChange
被评估,它是false
,作为它的初始值。 Afterwards the second operand is evaluated.然后评估第二个操作数。 It also returns
false
, even though it has some side-effect.它也返回
false
,即使它有一些副作用。 That side-effect however is irrelevant as the value of globalChange
already was evelauted before .然而,这种副作用是无关紧要的,因为
globalChange
的值之前已经被评估过。 Thus false | false
因此
false | false
false | false
just returns false
. false | false
只返回false
。
The ||
||
-operator on the other hand is short-circuiting, which means the right part is only evaluated if the left one is false
.另一方面, -operator 是短路的,这意味着仅在左侧为
false
时才评估右侧部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.