ternary-operator

Why assigning null in ternary operator fails: no implicit conversion between null and int?

落爺英雄遲暮 提交于 2019-11-27 07:37:54
问题 This fails with a There is no implicit conversion between 'null' and 'int' long? myVar = Int64.Parse( myOtherVar) == 0 ? null : Int64.Parse( myOtherVar); However, this succeeds: if( Int64.Parse( myOtherVar) == 0) myVar = null; else myVar = Int64.Parse( myOtherVar); Is there a way to make the ternary operator succeed? 回答1: The compiler ignores the left-hand side when figuring out the type of the right-hand side. So when it tries to deduce the type of Int64.Parse(myOtherVar) == 0 ? null : Int64

Precedence: Logical or vs. Ternary operator

随声附和 提交于 2019-11-27 07:21:45
问题 Consider the following: (EDIT: I've amended the function slightly to remove the use or braces with the ternary operator) function someFunction(start,end,step){ var start = start || 1, end = end || 100, boolEndBigger = (start < end); // define Boolean here step = step || boolEndBigger ? 1:-1; console.log(step); } someFunction() // step isn't defined so expect (1<10) ? 1:-1 to evaluate to 1 someFunction(1,10) // again step isn't defined so expect to log 1 as before The problem: someFunction(1

iif equivalent in c#

怎甘沉沦 提交于 2019-11-27 07:02:26
Is there a IIf equivalent in C# ? Or similar shortcut? C# has the ? ternary operator, like other C-style languages. However, this is not perfectly equivalent to IIf() ; there are two important differences. To explain the first difference, the false-part argument for this IIf() call causes a DivideByZeroException , even though the boolean argument is True . IIf(true, 1, 1/0) IIf() is just a function, and like all functions all the arguments must be evaluated before the call is made. Put another way, IIf() does not short circuit in the traditional sense. On the other hand, this ternary

Does the C/C++ ternary operator actually have the same precedence as assignment operators?

只谈情不闲聊 提交于 2019-11-27 06:54:50
问题 Almost all C/C++ operator precedence tables I have consulted list the ternary conditional operator as having higher precedence than the assignment operators. There are a few tables, however, such as the one on wikipedia, and the one at operator-precedence.com, that place them on the same precedence level. Which is it, higher or same? 回答1: In the C++ grammar, assignment-expression: conditional-expression logical-or-expression assignment-operator initializer-clause throw-expression conditional

Throw and ternary operator in C++

只愿长相守 提交于 2019-11-27 06:48:38
问题 The following code compiles with G++ 4.6.1, but not with Visual Studio 2008 return (m_something == 0) ? throw std::logic_error("Something wrong happened") : m_something; The fact is the Visual Studio compiler performs an internal crash. I want to know if this is standard C++ and why it doesn't compile with Visual Studio, but does with G++? 回答1: It is standard C++. Either (or both) of the then/else expressions in a conditional expression is allowed to be a throw-expression instead (C++98 5.16

What is the idiomatic Go equivalent of C's ternary operator?

陌路散爱 提交于 2019-11-27 06:10:46
In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator : int index = val > 0 ? val : -val Go doesn't have the conditional operator. What is the most idiomatic way to implement the same piece of code as above ? I came to the following solution, but it seems quite verbose var index int if val > 0 { index = val } else { index = -val } Is there something better ? As pointed out (and hopefully unsurprisingly), using if+else is indeed the idiomatic way to do conditionals in Go. In addition to

corresponding nested ternary operator in php?

怎甘沉沦 提交于 2019-11-27 05:41:09
I want to convert following if else condition to nested ternary operator. if ($projectURL) { echo $projectURL; } elseif ($project['project_url']) { echo $project['project_url']; } else { echo $project['project_id']; } I have written like following. echo ($projectURL)?$projectURL:($project['project_url'])?$project['project_url']: $project['project_id']; But it is found as not working properly.Is this not a right way? Ternary operators are tricky thing in PHP, as they are left-associative (unlike all other languages, where it's right-associative). You will need to use parenthesis to tell PHP

doing comparison if else in JasperReports

二次信任 提交于 2019-11-27 05:14:42
I want to do a comparison such as: if <field> == 0 then "-" Can somebody tell me the syntax using JasperReports? iReport (JasperReports) uses a Ternary operator . For example, consider the following logic: IF boolean condition THEN execute true code ELSE execute false code END IF Using a ternary operator, this becomes: boolean condition ? execute true code : execute false code When using a variable with the following expression: $F{column_value}.intValue() == 42 ? "Life, Universe, Everything" : "Naught" Then the variable's value would be "Life, Universe, Everything" if, and only if, the

Type result with conditional operator in C#

喜欢而已 提交于 2019-11-27 05:06:24
I am trying to use the conditional operator, but I am getting hung up on the type it thinks the result should be. Below is an example that I have contrived to show the issue I am having: class Program { public static void OutputDateTime(DateTime? datetime) { Console.WriteLine(datetime); } public static bool IsDateTimeHappy(DateTime datetime) { if (DateTime.Compare(datetime, DateTime.Parse("1/1")) == 0) return true; return false; } static void Main(string[] args) { DateTime myDateTime = DateTime.Now; OutputDateTime(IsDateTimeHappy(myDateTime) ? null : myDateTime); Console.ReadLine(); ^ } | } |

How to concatenate multiple ternary operator in PHP?

馋奶兔 提交于 2019-11-27 05:03:10
I use ternary operators alot but I can't seem to stack multiple ternary operator inside each other. I am aware that stacking multiple ternary operator would make the code less readable but in some case I would like to do it. This is what I've tried so far : $foo = 1; $bar = ( $foo == 1 ) ? "1" : ( $foo == 2 ) ? "2" : "other"; echo $bar; // display 2 instead of 1 What is the correct syntax ? Those parenthesis are what I think is getting you. Try $foo = 1; $bar = ($foo == 1) ? "1" : (($foo == 2) ? "2" : "other"); echo $bar; The problem is that PHP, unlike all other languages , makes the