conditional-operator

? operator without else-part

久未见 提交于 2019-12-01 02:30:40
I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements): if(someStatement) { someBool = true; //someBools value is unknown } else { //Do nothing } This can be achieved on a one-liner by doing: someBool = (someStatement) ? true : someBool; But why can't I do something like this: someBool = (someStatement) ? true : ; //or possibly someBool = (someStatement) ? true; Is this possible in some way? If it is, is there any benefits of using one method over the other? And if not, why shouldn't you be able to do it

Does F# have the ternary ?: operator?

北慕城南 提交于 2019-12-01 02:04:33
I'm learning F# coming from C# and I've just tried compiling an expression like let y = Seq.groupBy (fun x -> (x < p ? -1 : x == p ? 0: 1)) but see 'unexpected integer literal in expression'. Does F# have a ternary operator? If not, what should I use instead? Yes, it's called if .. then .. else In fact in F# everything is an expression, even an if .. then .. else block. In C# var x = true ? 0 : 1; In F# let x = if true then 0 else 1 So in your case: let y = Seq.groupBy (fun x -> if x < p then -1 else if x = p then 0 else 1) you can shorten it a bit with elif let y = Seq.groupBy (fun x -> if x

One-liner for if then [duplicate]

安稳与你 提交于 2019-12-01 01:16:06
This question already has an answer here: ternary operator in matlab 7 answers Is there a one-liner in MATLAB for this? if a > b foo = 'r'; else foo = 'g'; end Not as elegant as a C style ternary operator but you can take advantage of the fact that matlab will automatically cast logicals into doubles in this situation. So you can just multiply your desired result for true ( r in this case) by your condition ( a > b ), and add that to the product of your desired result for false (i.e. g ) with the not of your condition: foo = (a > b)*c + (~(a > b))*d so if we let c = 'r' and d = 'g' then all we

How to use EL conditional operator in JSF component attribute?

梦想的初衷 提交于 2019-12-01 00:37:52
I need to dynamically choose the width of a <p:panelGrid> based on a backing bean property. However it is not working for me. I have a feeling that I have some syntax error in my code. <p:panelGrid style="width:#{myBean.fromCCRM} ?70%:90%"> ... </p:panelGrid> siebz0r The entire statement needs to go inside #{...} . style="width:#{myBean.fromCCRM ? '70%' : '90%'}" 来源: https://stackoverflow.com/questions/12292397/how-to-use-el-conditional-operator-in-jsf-component-attribute

Ternary operator without the middle expression

扶醉桌前 提交于 2019-11-30 23:54:56
问题 I realized recently that you can use the ternary operator in GCC and clang without a middle ( ?: or ? : works) and it will insert the first expression into the middle: // outputs 2 cout << (2 ?: 4); // outputs 3 cout << (0 ? : 3); Where is this in the standard? I looked and didn't see anything about it. 回答1: It isn't in the standard at all . What you are observing is a GCC extension: https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html If you omit it, its value is taken from the first operand

? operator without else-part

筅森魡賤 提交于 2019-11-30 22:55:46
问题 I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements): if(someStatement) { someBool = true; //someBools value is unknown } else { //Do nothing } This can be achieved on a one-liner by doing: someBool = (someStatement) ? true : someBool; But why can't I do something like this: someBool = (someStatement) ? true : ; //or possibly someBool = (someStatement) ? true; Is this possible in some way? If it is,

Conditional operator doesn't work with two types that inherit the same base type

懵懂的女人 提交于 2019-11-30 22:49:38
问题 How come the conditional operator ( ?: ) doesn't work when used with two types that inherit from a single base type? The example I have is: ActionResult foo = (someCondition)? RedirectToAction("Foo","Bar") : Redirect(someUrl); Where the long form works fine: ActionResult foo; if(someCondition) { foo = RedirectToAction("Foo","Bar"); } else { foo = Redirect(someUrl); } Both return types, RedirectToRouteResult and RedirectResult , inherit from ActionResult . 回答1: How come the conditional

Does F# have the ternary ?: operator?

两盒软妹~` 提交于 2019-11-30 22:20:52
问题 I'm learning F# coming from C# and I've just tried compiling an expression like let y = Seq.groupBy (fun x -> (x < p ? -1 : x == p ? 0: 1)) but see 'unexpected integer literal in expression'. Does F# have a ternary operator? If not, what should I use instead? 回答1: Yes, it's called if .. then .. else In fact in F# everything is an expression, even an if .. then .. else block. In C# var x = true ? 0 : 1; In F# let x = if true then 0 else 1 So in your case: let y = Seq.groupBy (fun x -> if x < p

Yet Another Conditional Operator Nesting Question

若如初见. 提交于 2019-11-30 20:27:43
As per C precedence tables, the ternary conditional operator has right-to-left associativity. So, is it directly convertible to the equivalent if-else ladder? For example, can: x?y?z:u:v; be interpreted as: if(x) { if(y) { z; } else { u; } } else { v; } by matching an else ( : ) with the closest unpaired if ( ? )? Or does right-to-left associativity imply some other arrangement? interjay The example you gave could only be interpreted in one way (like the if statements you gave), whether the ternary operator had right-to-left or left-to-right associativity. Where the right-to-left associativity

Strange java behaviour with conditional operator. Is it a bug?

寵の児 提交于 2019-11-30 19:40:17
Can you please run the below and explain? Object o = true ? new Integer(1) : new Double(2.0); System.out.println(o); I found that surprising as someone would expect 1 to be printed and not 1.0 It's not a surprise at all, although it might seem like one. The behaviour is specified in JLS §15.25 - Conditional Operator : Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: If one of the operands is of type byte or Byte and the other is of type short or Short , then the type of the conditional expression is short . [..