ternary-operator

Ternary/null coalescing operator and assignment expression on the right-hand side?

房东的猫 提交于 2019-12-04 10:37:46
While experimenting with ternary and null coalesce operators in C# I discovered that it is possible to use assignments on the right-hand side of expressions, for example this is a valid C# code: int? a = null; int? b = null; int? c = a ?? (b = 12); int? d = a == 12 ? a : (b = 15); Strangely enough, not only the assignment on the right-hand side of the expression is evaluated to its own right-hand side (meaning that the third line here is evaluated to 12 and not to something like b = 12 => void ), but this assignment also effectively works, so that two variables are assigned in one statement.

Html literal in Razor ternary expression

徘徊边缘 提交于 2019-12-04 09:57:04
问题 I'm trying to do something like the following <div id="test"> @( string.IsNullOrEmpty(myString) ? @:  : myString ) </div> The above syntax is invalid, I've tried a bunch of different things but can't get it working. 回答1: Try the following: @Html.Raw(string.IsNullOrEmpty(myString) ? " " : Html.Encode(myString)) But I would recommend you writing a helper that does this job so that you don't have to turn your views into spaghetti: public static class HtmlExtensions { public static IHtmlString

Initialization of structure with ternary operator

狂风中的少年 提交于 2019-12-04 09:08:26
Why the ternary operator cannot be used to initialize a structure type, while it can be used to initialize a base type like int ? Example code : #include <stdio.h> #define ODD 1 int main(int argc, const char *argv[]) { static struct pair_str { int first; int second; } pair = ( ODD ) ? {1,3} : {2,4}; // ERROR printf("pair %d %d\n", pair.first, pair.second); int number = (ODD) ? 1 :2; // FINE return 0; } Compiler errors : /home/giuseppe/struct.c: In function ‘main’: /home/giuseppe/struct.c:12:23: error: expected expression before ‘{’ token /home/giuseppe/struct.c:12:29: error: expected

Java ternary operator precedence ? Different outputs given

落爺英雄遲暮 提交于 2019-12-04 04:02:31
问题 I have the below code producing this output ( no space after -1 ) ==> "1 3 -14 -15 -1" int [] arr = {1, 3, Integer.MAX_VALUE, 4, Integer.MAX_VALUE, 5, Integer.MAX_VALUE}; for (int dist : arr) { System.out.print((dist == Integer.MAX_VALUE) ? -1 : dist + " "); } But if I evaluate the ternary expression separately (as shown below), it gives a different output (what I expected) ==> "1 3 -1 4 -1 5 -1" int [] arr = {1, 3, Integer.MAX_VALUE, 4, Integer.MAX_VALUE, 5, Integer.MAX_VALUE}; for (int dist

How can I closely achieve ?: from C++/C# in Python?

╄→尐↘猪︶ㄣ 提交于 2019-12-04 04:00:41
In C# I could easily write the following: string stringValue = string.IsNullOrEmpty( otherString ) ? defaultString : otherString; Is there a quick way of doing the same thing in Python or am I stuck with an 'if' statement? In Python 2.5, there is A if C else B which behaves a lot like ?: in C. However, it's frowned upon for two reasons: readability, and the fact that there's usually a simpler way to approach the problem. For instance, in your case: stringValue = otherString or defaultString @Dan if otherString: stringValue = otherString else: stringValue = defaultString This type of code is

Result values in '? :' expression have mismatching types '()' and 'Bool' [duplicate]

雨燕双飞 提交于 2019-12-04 03:32:50
This question already has an answer here: Swift ternary operator compilation error 2 answers I have an array of Doubles, and a button which when pressed empties the array. I want the button to be enabled only when the count of the array is greater than zero. The code is the following: var numbers: [Double] = [] //At some point I add some numbers here numbers.count > 0 ? deleteAllNumbersButton.isEnabled = true : deleteAllNumbersButton.isEnabled = false The compiler complains: Result values in '? :' expression have mismatching types '()' and 'Bool' When put in an if statement it works just fine

Ternary Operator in JavaScript With Multiple Expressions?

一个人想着一个人 提交于 2019-12-04 02:57:49
问题 the_styles ? the_styles.appendTo('head'); the_styles=null : the_styles = $('.stylesheet').detach(); Obviously, this isn't valid. Notice the ";" between the appendTo() and the_styles=null . How do I write it on 1 line and still have multiple expressions like that? 回答1: Use the comma operator this way: the_styles ? (the_styles.appendTo('head'), the_styles=null) : the_styles = $('.stylesheet').detach(); Here's what the Mozilla Developer Center writes about the comma operator: You can use the

Why ternary operator in swift is so picky?

匆匆过客 提交于 2019-12-04 02:56:21
问题 The question is very simple, but I just could not find the answer! Why doesn't return x == 0? "" : "Hello" compile but return x == 0 ? "" : "Hello" does? This is really weird because all the other operators don't need an extra white space. e.g. let x = 1+1 let y = 1 + 1 are the same. I think it has something to do with optionals. But when you use a ? operator on a variable, it must be used like this: let s: String? = nil let x = s?.startIndex I mean it must follow another operator, right? 回答1

Ternary operator usage in Scala program [closed]

删除回忆录丶 提交于 2019-12-04 02:42:50
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I have a Array of Objects that I need to apply filter. val filteredList = list.filter{ l => (pid == "") ? true : l.ProviderId.toUpperCase().contains(pid.toUpperCase()))} This code is not getting complied by Scala compiler. I am getting error like 1) value ? is not a member of boolean 2) type toUpperCase is not a

Unexpected output when using a ternary operator and final variable

最后都变了- 提交于 2019-12-04 02:21:06
Consider this code snippet: public static void main(String[] args) { int z1 = 0; final int z2 = 0; System.out.println(false ? z1 : 'X'); System.out.println(false ? z2 : 'X'); } When running this code, I would expect to see two X in your console. However, the real output is: 88 X If we take a look at the Java specifications regarding the ternary operator , we found that If one of the operands is of type T where T is byte, short, or char, and the other operand is a constant expression of type int whose value is representable in type T, then the type of the conditional expression is T. So the