ternary-operator

Ternary operation in CoffeeScript

风格不统一 提交于 2019-11-27 16:56:11
I need to set value to a that depends on a condition. What is the shortest way to do this with CoffeeScript? E.g. this is how I'd do it in JavaScript: a = true ? 5 : 10 # => a = 5 a = false ? 5 : 10 # => a = 10 Since everything is an expression, and thus results in a value, you can just use if/else . a = if true then 5 else 10 a = if false then 5 else 10 You can see more about expression examples here . a = if true then 5 else 10 a = if false then 5 else 10 See documentation . In almost any language this should work instead: a = true && 5 || 10 a = false && 5 || 10 Coffeescript doesn't support

Java ternary operator (?:) doesn't work; second or third operand return boolean

和自甴很熟 提交于 2019-11-27 16:21:18
Can someone tell me why this use of the ternary operator is incorrect? Operands 2 and 3 return a boolean. public class Something { ... private static final double REFERENCE_FRAME_MID_X = 0; private static final double REFERENCE_FRAME_MID_Y = 0; private boolean findInsideOrOutsideGeneralEllipse(Point2D destCirclePos) { List<Boolean> returnValue = new ArrayList<>(); Point2D referenceFrameCenter = new Point2D.Double(REFERENCE_FRAME_MID_X, REFERENCE_FRAME_MID_Y); Ellipse2D insideAreaEllipse2D = getEllipse2D(referenceFrameCenter.getX(), referenceFrameCenter.getY(), destCirclePos.distance

Ternary Expression with Interfaces as a Base Class

≯℡__Kan透↙ 提交于 2019-11-27 16:05:23
I am attempting to create a ternary expression and I am getting the following error "Type of conditional expression cannot be determined because there is no implicit conversion between LiveSubscription and DisconnectedSubscription" The same logic works in an if statement, but I wanted to understand why it won't work in a ternary expression - Here is the gist of what I am trying to do: public interface IClientSubscription { bool TryDisconnect(); } public class LiveSubscription : IClientSubscription { public bool TryDisconnect() { return true; } } public class DisconnectedSubscription :

PHP Shorthand ternary operator “?:” Parse error unexpected “:”

柔情痞子 提交于 2019-11-27 15:33:16
问题 I've just uploaded some old PHP files to a new server and am getting parse errors (Unexpected ':') on shorthand ternary ops. eg: $y = $x ?: "Some default"; php version is 5.2.16 The code is littered with these shorthand ?:, so before changing them all I thought I'd see if anyone knows anything about this as I've not used PHP for a while now. 回答1: This is only available since PHP 5.3 The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE , and expr3 if expr1

java ternary operator

折月煮酒 提交于 2019-11-27 14:52:38
Can someone explain why this code? Collection c = (5 == 5) ? new ArrayList() : new HashSet(); produces the following compiler error: Incompatible conditional operand types ArrayList and HashSet For reasons that I don't understand, the following fixes the problem Collection c = (5 == 5) ? (Collection) new ArrayList() : new HashSet(); I'm using Java 1.4. This was a bug in 1.4 and has been fixed according bugreport 5080917 . Evaluation This is a bug. xxxxx@xxxxx 2004-07-30 Daniel more or less gets this right, but has deleted his answer (with five up votes). Relevant quote from 2nd Ed JLS (1.2-1.4

Is it safe to create a const reference to result of ternary operator in C++?

跟風遠走 提交于 2019-11-27 14:42:48
问题 There's something quite non-obvious going on in this code: float a = 1.; const float & x = true ? a : 2.; // Note: `2.` is a double a = 4.; std::cout << a << ", " << x; both clang and gcc output: 4, 1 One would naively expect the same value printed twice but this isn't the case. The issue here has nothing to do with the reference. There are some interesting rules dictating the type of ? : . If the two arguments are of different type and can be casted, they will by using a temporary. The

Simple PHP isset test

╄→尐↘猪︶ㄣ 提交于 2019-11-27 14:38:50
问题 This below does not seem to work how I would expect it, event though $_GET['friendid'] = 55 it is returning NULL <?PHP $_GET['friendid'] = 55; $friendid = (!isset($_GET['friendid'])) ? $_GET['friendid'] : 'empty'; echo $friendid; exit; ?> 回答1: Remove the ! . You don't want to negate the expression. $friendid = isset($_GET['friendid']) ? $_GET['friendid'] : 'empty'; 回答2: As of PHP 7's release, you can use the null-coalescing operator (double "?") for this: $var = $array["key"] ?? "default

Using ternary operator in JavaScript to invoke two functions

眉间皱痕 提交于 2019-11-27 14:21:26
Can this be done in JavaScript? type == 1 ? function1() : function2(); Yes, that's valid code. It will invoke either function1() or function2() , but not both - depending on the value of type . It won't invoke two functions. It will invoke one of your two functions. 来源: https://stackoverflow.com/questions/1655037/using-ternary-operator-in-javascript-to-invoke-two-functions

Ternary operator in JSTL/EL

删除回忆录丶 提交于 2019-11-27 13:52:10
The following tag of JSTL can be used to set a value to a variable in a request scope. <c:set var="value" scope="request" value="someValue"/> I want to check conditionally, if the variable value being set is empty or not and display the result accordingly something like the following, using <c:when>...</c:when> . <c:choose> <c:when test="${not empty value}"> <c:out default="None" value="${value}"/> </c:when> <c:otherwise> <c:out default="None" value="None"/> </c:otherwise> </c:choose> I want to reduce the line of code using a ternary expression like, <c:out default="None" value="${not empty

Why doesn't this method work? Java ternary operator

﹥>﹥吖頭↗ 提交于 2019-11-27 13:40:32
What's wrong with this code: void bark(boolean hamlet) { hamlet ? System.out.println("To Bark.") : System.out.println("Not to Bark"); } Ternary operators can't have statements that don't return values, void methods. You need statements that have return values. You need to rewrite it. void bark(boolean hamlet) { System.out.println( hamlet ? "To Bark." : "Not to Bark" ); } You can read why in the Java Language Specification, 15.25. Conditional Operator ? : It is a compile-time error for either the second or the third operand expression to be an invocation of a void method. You need to do as