为什么条件运算符不能用作语句
为什么条件运算符不能用作语句?
我想做类似的事情:
boolean isXyz = ...;
...
isXyz ? doXyz() : doAbc();
其中 doXyz
和 doAbc
返回 void。
请注意,这与其他运算符不同,例如 doXyz() + doAbc() 本质上需要 doXyz 和 doAbc 返回一个类似数字的东西来操作(或者要连接的字符串,或者其他什么,但重点是 + 实际上需要进行操作的值)。
是否有什么深刻的东西或者这只是一个武断的决定。
注意:我来自 Java 世界,但我想知道这在您最喜欢的编程语言中是否可行。
Why can't the conditional operator be used as a statement?
I would like to do something like:
boolean isXyz = ...;
...
isXyz ? doXyz() : doAbc();
where doXyz
and doAbc
are return void.
Note that this is not the same as other operators, for example doXyz() + doAbc() intrinsically needs that doXyz and doAbc return a number-like something to operate (or strings to concatenate, or whatever, but the point is that + actually needs values to operate on).
Is there something deep or is it just an arbitrary decision.
Note: I come from the Java world, but I would like to know if this is possible in your favourite programming language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C
和C++
确实允许这样的构造。只要doXyz()
和doAbc()
返回相同的类型。包括void
。C
andC++
do allow such constructs. As long asdoXyz()
anddoAbc()
return the same type. Includingvoid
.有什么意义呢?为什么不直接使用 if 语句(在我看来,它看起来更干净)?
What would be the point? Why not just use an if statement (which, in my opinion, looks cleaner)?
因为它会降低可读性并引入潜在的错误。
语言提供了使用关键字“if”来做你想做的事情的方法。
一条语句应该只执行一些操作。
条件运算符旨在返回一个值。
Because it would reduce readability and introduce a potential for errors.
Languages offer means of doing what you wish by using the keyword "if".
A statement is supposed to just perform some operations.
A conditional operator is meant to return a value.
作为一个新颖之处,mIRCscripting 允许您使用
/canI? 调用它 来执行此操作。 1
将回显this can
。使用
/canI 调用它? 2
将回显that can
。As a novelty, mIRCscripting allows you to do this
calling it with
/canI? 1
will echothis can
.calling it with
/canI? 2
will echothat can
.这不是和
if
语句完全相同吗?有些语言确实允许您使用条件运算符作为语句。我想到了 Perl。
Wouldn't this be exactly the same as the
if
statement?Some languages do allow you to use the conditional operator as a statement. Perl comes to mind.
表达式(包括条件表达式)可以单独用作 Java 和许多其他语言(我想说的是当前最流行的语言)中的语句。
这里的问题是 Java 中的“void-returning”,与条件无关。有时,在表达式中隐藏活动(非幂等;有副作用)代码被认为是不好的品味,并且活动函数通常返回 void。因此,由于 Java 是一种规范性语言,因此它不允许在表达式中使用 void 函数。许多其他语言更加宽松并且允许这样做。
您可以通过让 doAbc 和 doXyz 返回一些东西来解决这个问题 - 零,布尔值,任何东西:只要它们的类型相同就没关系,结果将在 ExpressionStatement 中被丢弃。但我真的不知道你为什么想要这样做;正如其他人所说,这确实是一种表达方式低俗且毫无意义的情况。
An expression, including a conditional expression, may be used on its own as a statement in Java and many other languages (I'd go as far as to say most currently-popular languages).
It's specifically ‘void-returning’ in Java that's the issue here, not anything to do with conditionals. It's sometimes considered bad taste to hide active (non-idempotent; with side-effects) code inside an expression, and active functions often return void. So because Java is a prescriptive language, it disallows using a void function in an expression. Many other languages are more permissive and will allow it.
You could get around it by having doAbc and doXyz return something — zero, a boolean, anything: it doesn't matter as long as they're the same type for both, the result will be thrown away in an ExpressionStatement. But I don't really know why you'd want to; as others have said, this is indeed a case where doing it in an expression is in poor taste and largely pointless.
我认为你的问题是错误的......添加条件运算符是因为“IF THEN”语句不能用作评估语句。
在我的选择中,您应该只在条件评估时使用条件运算符,因为它本质上不如纯粹实现条件时使用“IF THEN”结构清晰。
条件运算符通常不能在每个条件结果上包含多个指令块,但“IF THEN”可以。
I think your question is the wrong way round.... the conditional operator was added because the "IF THEN" statement couldn't be used as an evaluation statement.
In my option you should only use the conditional operator when conditionally evaluating as it is inherently less clear than using "IF THEN" constructs when purely implementing a condition.
Conditional operators cannot typically contain blocks of multiple instructions on each condition result, "IF THEN" can.