Ternary expression with defined? returns expression instead of value?
defined?
is binding to amount ? amount : r(1,4)
so it is equivalent to:
defined?(amount ? amount : r(1,4))
You probably want:
defined?(amount) ? amount : r(1,4)
Actually, odds are that amount || r(1,4)
, or amount.nil? ? r(1,4) : amount
would better match what you want, since I think you don't want this:
1.9.3p194 :001 > defined?(amount)
=> nil
1.9.3p194 :002 > amount = nil
=> nil
1.9.3p194 :003 > defined?(amount)
=> "local-variable"
...in which case @c
would be nil
- the value of the defined variable.
Why can't we have return in the ternary operator?
The ternary operator evaluates to an expression and expressions can't contain a return statement (how would that behave if you were to assign the expression to a variable?). However, you could very well return the result of a ternary operator, i.e. return condition? returnValue1 : returnValue2;
On your specific point, I don't see why you would like to return. It looks like you're trying to do something only if a condition is fulfilled. A simple if
statement would probably be more adequate there.
UnexpectedToken error when I try to use return with ternary operator but not with if statement?
Why does this happen?
Because return
is a statement, and you can't put statements where only expressions are allowed.¹ The conditional operator's operands must be expressions, not statements. (You'd have the same problem with throw
, just FWIW, though there's talk of adding a throw
operator... Or with while
or for
or any other statement.)
The if
is probably your best bet.
If you were really, really intent on just using a single expression, you could put the return
at the start so the single expression is what follows it:
function monthName(nr) {
return 0 < nr < 13 ? alert("Month: " + months[nr]) : false;
}
Note that that relies on the fact that alert
returns undefined
in order to maintain the two return values you were providing in your if
example (undefined
if the alert
was shown, false
if not). Interestingly, the spec doesn't explicitly say that alert
returns undefined
but I think we can safely accept that it's implicitly specified (not least because the implicit return of any non-arrow JavaScript function is undefined
unless return <value>
is used).
¹ (You can do the opposite; JavaScript has the concept of the "expression statement" which means any expression is also a valid statement.)
Alternative for ternary operator in the case a ? a : b without repetition (JavaScript)
You could take a default approach with a logical OR ||
. This returns a
, if this values is truthy, otherwise the default value of b
.
a || b
Why do ternary operator and if statement return different results?
In the ternary version, it is interpreting your default
as default(DateTimeOffset)
, the other output expression type in the conditional. Then it is interpreting the ternary as a whole as a nullable, which will never be null.
In the second case, your return is using default(DateTimeOffset?)
, from the declared return type.
In this case, you may want to use an explicit type in the default
expression, or: just use the second form and add a comment (and ideally a unit test), so nobody "fixes" it in the future.
Ternary operators and Return in C
return
is a statement. Statements cannot be used inside expressions in that manner.
Related Topics
Parsing Uris That Have Curly Braces, Uri::Invalidurierror: Bad Uri(Is Not Uri)
Http.Post_Form in Ruby with Custom Headers
Regular Expression to Match Only the First File in a Rar File Set
Sublime Text Can't Understand Gets.Chomp
Should Repeat a Number of Times
P Method in Ruby Hard to Search For
Install Gem Gives "Failed to Build Gem Native Extension."
Ruby Code for Modifying Outer Quotes on Strings
Rails Cannot Find Model with Same Name as Ruby Class
Failing to Enable User-Env-Compile on Heroku
How to Create a "Clone"-Able Enumerator for External Iteration
Pixel Rgb with Imagemagick and Rails
Adapters Not Working with Datamapper
Handling Has_One Nested Resource in Rails 3
Copy One Slide from Google Slides into a New Presentation Using API
Twitter::Error::Unauthorized in Postscontroller#Create - Invalid or Expired Token