我们最近在生产中遇到了一个错误,我发现它非常有趣。我长期以来的理解是 Java 布尔值只能是 false 或 true。然而,在三元运算符中,它似乎最终可以解析为 null,并且它从未产生编译错误,并且一直构建到生产环境。我很惊讶以下代码不会生成编译错误。有谁知道为什么它编译得很好?恕我直言,它不应该编译!它最终解析的值是一个原生布尔值。
boolean por = (str == null || str.length() == 0) ? null : "true".equalsIgnoreCase(str);
Run Code Online (Sandbox Code Playgroud) 我有 Julia 代码,如图所示
if cxp1_v1 < cxp1_v2
d_min = cxp1_v1
d_max = cxp1_v2
else
d_min = cxp1_v2
d_max = cxp1_v1
end
Run Code Online (Sandbox Code Playgroud)
或者
if cxp1_v1 < cxp1_v2 d_min, d_max = cxp1_v1, cxp1_v2 else d_min, d_max = cxp1_v2, cxp1_v1 end
Run Code Online (Sandbox Code Playgroud)
有没有办法在三元运算符中完成同样的任务?我尝试了如图所示的东西
cxp1_v1 < cxp1_v2 ? d_min, d_max = cxp1_v1, cxp1_v2:d_min, d_max = cxp1_v2, cxp1_v1
Run Code Online (Sandbox Code Playgroud)
但我得到
syntax: colon expected in "?" expression
Stacktrace:
[1] top-level scope
@ In[422]:11
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
Run Code Online (Sandbox Code Playgroud) 条件表达式允许 throw 表达式作为操作数。我想要一个条件表达式,其中操作数之一是始终抛出的函数,但似乎这是不可能的。
#include<exception>
[[ noreturn ]] void foo() {
throw std::exception();
}
int main() {
int a = true ? 1 : throw std::exception();
int b = true ? 1 : foo(); // This will not compile
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过内联 foo,但它也无法编译。错误是一样的:
test.cc: In function 'int main()':
test.cc:9:18: error: third operand to the conditional operator is of type 'void', but the second operand is neither a throw-expression nor of type 'void'
int b = true ? 1 : foo(); // This …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main()
{
int a=4;
int b=4;
int c= a++ < ++b? 1 : 0;
printf ("%d",c);
}
Run Code Online (Sandbox Code Playgroud)
已知在 处有一个序列点?,这意味着前缀和后缀操作都必须在该点完成。还知道(?)b在比较之前增加。然而,是a在比较之前还是之后增加呢?
如果在测试之前递增<,则布尔值计算结果为 false 并c设置为 0,否则为 true 并c设置为 1。在我的编译器中,它计算结果为 true,这意味着a++在比较操作之后执行并c设置为1.
但这种行为是规范的一部分吗?
我修改为
#include<stdio.h>
int main()
{
int a=4;
int b=4;
int d=2;
int c= a++ + d < ++b + d? 1 : 0;
printf ("%d",c);
}
Run Code Online (Sandbox Code Playgroud)
它的计算结果仍然为 1。后缀必须在 之前完成?,但这真的确保它在比较之后发生吗<?
c conditional-operator unary-operator sequence-points postfix-operator
SoftUpdateAsync使用此代码,对and的调用UpdateAsync似乎没有等待,我收到了死锁事务的错误。
_ = await _tabRepository.IsReferencedFromAnotherTableAsync(entity.Id)
? _tabRepository.SoftUpdateAsync(entity)
: _tabRepository.UpdateAsync(entity);
Run Code Online (Sandbox Code Playgroud)
该函数IsReferencedFromAnotherTableAsync返回一个布尔值,UpdateAsync并且SoftUpdateAsync返回一个任务。
但是,如果我将代码写为
if (await _tabRepository.IsReferencedFromAnotherTableAsync(entity.Id))
await _tabRepository.SoftUpdateAsync(entity);
else
await _tabRepository.UpdateAsync(entity);
Run Code Online (Sandbox Code Playgroud)
一切似乎都正常工作。
我可以使用三元运算符代替 if then else 吗?
我尝试过:
_ = await _countdownRepository.IsReferencedFromAnotherTableAsync(entity.Id)
? await _countdownRepository.SoftUpdateAsync(entity)
: await _countdownRepository.UpdateAsync(entity);
Run Code Online (Sandbox Code Playgroud)
和
await _countdownRepository.IsReferencedFromAnotherTableAsync(entity.Id)
? await _countdownRepository.SoftUpdateAsync(entity)
: await _countdownRepository.UpdateAsync(entity);
Run Code Online (Sandbox Code Playgroud)
两者都会导致错误:
第一个是“不能对 void 类型的值进行赋值”,第二个是“只有赋值、调用、递增、递减和新对象表达式可以用作语句”
可能重复:
php == vs === operator
PHP中的!==和!=有什么区别?
我使用安全导航操作符来避免像这样的NPE
myInstance?.field1
myInstance?.field2
myInstance?.field3
Run Code Online (Sandbox Code Playgroud)
要转换上面的代码for for loop我写道
<g:each var="i" in="${ (1..<4 }">
myInstance['field'+i]
</g:each>
Run Code Online (Sandbox Code Playgroud)
请考虑以下代码构造:
condition ? code_if_true :
condition2 ? code_if_true2 :
code_if_false;
Run Code Online (Sandbox Code Playgroud)
这不适用于PHP,而适用于JavaScript。
有没有一种方法可以使此功能适用于PHP?
我想知道为什么与三元运算符的赋值反应奇怪:
a = "foo"
=> "foo"
a = nil ? nil : a
=> "foo"
a
=> "foo"
Run Code Online (Sandbox Code Playgroud)
但:
a = nil ? nil : a
=> "foo"
a = "bar" ? "bar" : a
=> "bar"
a
=> "bar"
Run Code Online (Sandbox Code Playgroud)
和:
if a = nil
puts "should be nil"
end
=> nil
Run Code Online (Sandbox Code Playgroud)
不会puts是字符串,因为虽然作业成功,但a = nil会返回nil错误.
这一切都表现得像预期的那样吗?
使用以下代码
import sys
print "Hello " + sys.argv[1] if len(sys.argv) > 1 else "Joe" + "."
Run Code Online (Sandbox Code Playgroud)
当我跑
python hello.py,我明白了Joe.- Hello失踪了.python hello.py Nick,我得到Hello Nick- 期间'.' 最后遗失了.但是,当我更改代码以覆盖三元操作时(),它可以正常工作.
import sys
print "Hello " + (sys.argv[1] if len(sys.argv) > 1 else "Joe") + "."
Run Code Online (Sandbox Code Playgroud)
当我跑
python hello.py我明白了 Hello Joe.python hello.py Nick我明白了 Hello Nick.当我没有用括号覆盖三元运算符时,有人可以解释为什么不同的行为.