question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Elvis operator in expressions: `?.`

See original GitHub issue

Currently a.b.c will thrown an error in expression if a is null. To suppress null dereferencing we should support a?.b.c which would short circuit further evaluation if a is null. In this way we could have explicit notation which would show which values could be null.

Syntax to fallow dart proposal: https://github.com/gbracha/nullAwareOperators/blob/master/README.md

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:2
  • Comments:45 (33 by maintainers)

github_iconTop GitHub Comments

11reactions
jelbourncommented, Mar 3, 2015

Feels kind of weird to me; it’s surprising to have language extensions within the binding expression syntax.

This might also need some more thinking, but I don’t see this as being that useful. If angular defaults to gracefully handling null dereferencing in expression then the developer doesn’t have to care whether something is loaded lazy or not. The times when people do care is when something needs to be shown in place of the pending data, which will still be handled with an if

6reactions
leonard84commented, Jan 28, 2016

@vicb @mhevery Sorry for resurrecting this but I want to point out that the safe-navigation-operator and the “Elvis” operator are two different things. See Wikipedia , Groovy-Elvis, Groovy-Safe-Navigation. The Elvis operator is more akin to the Null coalescing operator with the difference that it tests for “truthy” instead of null for the lhs.

displayName = user.name ? user.name : 'Anonymous'   
displayName = user.name ?: 'Anonymous'                  // <- Elvis operator

I think it is bad practice to take established terminology from other languages and change its meaning. What angular currently has is a safe-navigation-operator and not the Elvis operator. Calling it Angular “Elvis” operator ( ?. ) does not make it better 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

Elvis operator - Wikipedia
In certain computer programming languages, the Elvis operator, often written ?: , is a binary operator that returns its first operand if that...
Read more >
Expressions And Operators: Ternary
The ternary operator ? : is a shorthand for if statements. It is an expression, so it evaluates to a value. For example:...
Read more >
Spring SpEL - Elvis Operator - Tutorialspoint
SpEL expression supports Elvis operator which is a short form of ternary operator. // Using ternary operator String result = name != null...
Read more >
Kotlin Elvis Operator - Javatpoint
Elvis operator (?:) is used to return the not null value even the conditional expression is null. It is also used to check...
Read more >
What does ?: do in Kotlin? (Elvis Operator) - Stack Overflow
In certain computer programming languages, the Elvis operator ?: is a binary operator that returns its first operand if that operand is true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found