Why a conditional statement cannot be used as an operator

Why can't a conditional operator be used as an operator?

I would like to do something like:

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

where doXyzand doAbcreturn invalid.

Note that this is not the same as other operators, for example doXyz () + doAbc () essentially needs doXyz and doAbc to return a number-something to work (or strings for concatenation or something yet, but the point is that + really needs values ​​to work).

Is there anything deep or is it just an arbitrary decision.

Note . I came from the Java world, but I would like to know if this is possible in your favorite programming language.

+3
7

C C++ . doXyz() doAbc() . void.

+5

? if (, , )?

+2

.

, , "if".

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

isXyz ? doXyz() : doAbc();

.

, .

+1

, 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.

+1

, if?

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

. Perl .

0

, , Java ( , ).

"void-return in Java, , - . , (-, ) , void. Java , void . .

, doAbc doXyz - - , , : , , ExpressionStatement. , ; , , .

0

, .... , "IF THEN" .

, , "IF THEN" .

, " " .

0

Source: https://habr.com/ru/post/1716774/


All Articles