我今天在一些PHP代码中看到了这个:
$items = $items ?: $this->_handle->result('next', $this->_result, $this);
Run Code Online (Sandbox Code Playgroud)
我不熟悉?:这里使用的运算符.它看起来像一个三元运算符,但是省略了判断谓词是否为真的表达式.这是什么意思?
我遇到了以下几行
hsb.s = max != 0 ? 255 * delta / max : 0;
Run Code Online (Sandbox Code Playgroud)
什么的?,并:在这方面是什么意思?
为什么第一个返回引用?
int x = 1;
int y = 2;
(x > y ? x : y) = 100;
Run Code Online (Sandbox Code Playgroud)
而第二个不?
int x = 1;
long y = 2;
(x > y ? x : y) = 100;
Run Code Online (Sandbox Code Playgroud)
实际上,第二个根本没有编译 - "没有左边的赋值".
让我们看看以下代码段中的简单Java代码:
public class Main {
private int temp() {
return true ? null : 0;
// No compiler error - the compiler allows a return value of null
// in a method signature that returns an int.
}
private int same() {
if (true) {
return null;
// The same is not possible with if,
// and causes a compile-time error - incompatible types.
} else {
return 0;
}
}
public static void main(String[] args) {
Main m = …Run Code Online (Sandbox Code Playgroud) 据我所知,PowerShell似乎没有所谓的三元运算符的内置表达式.
例如,在支持三元运算符的C语言中,我可以编写如下内容:
<condition> ? <condition-is-true> : <condition-is-false>;
Run Code Online (Sandbox Code Playgroud)
如果在PowerShell中确实不存在,那么实现相同结果的最佳方法(即易于阅读和维护)是什么?
我个人是三元运算符的拥护者:()?:; 我确实意识到它有它的位置,但我遇到了许多完全反对使用它的程序员,而且有些人经常使用它.
你有什么感受?你看到了什么有趣的代码?
我想只在我的字符串有一个有意义的值时执行一些操作.所以,我试过这个.
if (!myString.equals("")) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)
还有这个
if (!myString.equals(null)) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)
还有这个
if ( (!myString.equals("")) && (!myString.equals(null))) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)
还有这个
if ( (!myString.equals("")) && (myString!=null)) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)
还有这个
if ( myString.length()>0) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)
在所有情况下,我的程序doSomething尽管我的字符串是空的.它等于null.那么,那有什么问题?
添加:
我找到了问题的原因.该变量被声明为一个字符串,因此,null分配给该变量的变换为"null"!所以,if (!myString.equals("null"))有效.
我知道在C#中使用Null合并运算符的标准方法是设置默认值.
string nobody = null;
string somebody = "Bob Saget";
string anybody = "";
anybody = nobody ?? "Mr. T"; // returns Mr. T
anybody = somebody ?? "Mr. T"; // returns "Bob Saget"
Run Code Online (Sandbox Code Playgroud)
但还有什么可以??用于?它不像三元运算符那样有用,除了比以下更简洁和更容易阅读:
nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // returns Bob Saget
Run Code Online (Sandbox Code Playgroud)
所以考虑到甚至更少知道空合并运算符......
你有没有用过??别的东西?
是??必要的,还是应该只使用三元运算符(大多数人都熟悉)
c# null coding-style conditional-operator null-coalescing-operator
有人可以解释为什么这在C#.NET 2.0中有效:
Nullable<DateTime> foo;
if (true)
foo = null;
else
foo = new DateTime(0);
Run Code Online (Sandbox Code Playgroud)
......但这不是:
Nullable<DateTime> foo;
foo = true ? null : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)
后一种形式给我一个编译错误"无法确定条件表达式的类型,因为'<null>'和'System.DateTime'之间没有隐式转换."
并不是说我不能使用前者,但第二种风格与我的其余代码更加一致.
它更多的是关于python列表理解语法.我有一个列表理解,可以生成给定范围的奇数列表:
[x for x in range(1, 10) if x % 2]
Run Code Online (Sandbox Code Playgroud)
这是一个过滤器 - 我有一个源列表,我删除偶数(if x % 2).我想在这里使用if-then-else之类的东西.以下代码失败:
>>> [x for x in range(1, 10) if x % 2 else x * 100]
File "<stdin>", line 1
[x for x in range(1, 10) if x % 2 else x * 100]
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
有一个像if-else的python表达式:
1 if 0 is 0 else 3
Run Code Online (Sandbox Code Playgroud)
如何在列表理解中使用它?
python list-comprehension ternary-operator conditional-operator
c# ×2
coding-style ×2
java ×2
null ×2
autoboxing ×1
c++ ×1
equals ×1
generics ×1
javascript ×1
lvalue ×1
multilingual ×1
nullable ×1
operators ×1
php ×1
powershell ×1
python ×1
reference ×1
string ×1
syntax ×1
types ×1