为什么条件运算符不能用作语句

发布于 2024-08-04 07:26:10 字数 390 浏览 10 评论 0原文

为什么条件运算符不能用作语句?

我想做类似的事情:

boolean isXyz = ...;
...
isXyz ? doXyz() : doAbc();

其中 doXyzdoAbc 返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

甜扑 2024-08-11 07:26:10

CC++ 确实允许这样的构造。只要 doXyz()doAbc() 返回相同的类型。包括void

C and C++ do allow such constructs. As long as doXyz() and doAbc() return the same type. Including void.

晨敛清荷 2024-08-11 07:26:10

有什么意义呢?为什么不直接使用 if 语句(在我看来,它看起来更干净)?

What would be the point? Why not just use an if statement (which, in my opinion, looks cleaner)?

兰花执着 2024-08-11 07:26:10

因为它会降低可读性并引入潜在的错误。

语言提供了使用关键字“if”来做你想做的事情的方法。

// Is not much longer than the line below
// but significantly more transparent
if (isXyz) doXyz() else doAbc();

isXyz ? doXyz() : doAbc();

一条语句应该只执行一些操作。

条件运算符旨在返回一个值。

Because it would reduce readability and introduce a potential for errors.

Languages offer means of doing what you wish by using the keyword "if".

// Is not much longer than the line below
// but significantly more transparent
if (isXyz) doXyz() else doAbc();

isXyz ? doXyz() : doAbc();

A statement is supposed to just perform some operations.

A conditional operator is meant to return a value.

迷爱 2024-08-11 07:26:10

作为一个新颖之处,mIRCscripting 允许您使用

alias canI? {
   $iif($1 == 1,doThis,doThat)
}
alias doThis echo -a this can.
alias doThat echo -a that can.

/canI? 调用它 来执行此操作。 1 将回显this can
使用 /canI 调用它? 2 将回显that can

As a novelty, mIRCscripting allows you to do this

alias canI? {
   $iif($1 == 1,doThis,doThat)
}
alias doThis echo -a this can.
alias doThat echo -a that can.

calling it with /canI? 1 will echo this can.
calling it with /canI? 2 will echo that can.

泛泛之交 2024-08-11 07:26:10

这不是和 if 语句完全相同吗?

if (isXyz) doXyz(); else doAbc();

有些语言确实允许您使用条件运算符作为语句。我想到了 Perl。

Wouldn't this be exactly the same as the if statement?

if (isXyz) doXyz(); else doAbc();

Some languages do allow you to use the conditional operator as a statement. Perl comes to mind.

寄意 2024-08-11 07:26:10

表达式(包括条件表达式)可以单独用作 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.

花开雨落又逢春i 2024-08-11 07:26:10

我认为你的问题是错误的......添加条件运算符是因为“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文