Questions tagged [safe-navigation-operator]

28 questions
272
votes
22 answers

Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?

I'll explain by example: Elvis Operator (?: ) The "Elvis operator" is a shortening of Java's ternary operator. One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to false or null. A…
tiagomac
  • 2,935
  • 2
  • 15
  • 6
27
votes
3 answers

Safe navigation operator (&.) for nil

As Ruby 2.3 introduces the Safe navigation operator(&.), a.k.a lonely operator, the behavior on nil object seems odd. nil.nil? # => true nil&.nil? # => nil Is that designed to behave like this way? Or some edge case that slipped away when…
sbs
  • 4,102
  • 5
  • 40
  • 54
26
votes
7 answers

Safely assign value to nested hash using Hash#dig or Lonely operator(&.)

h = { data: { user: { value: "John Doe" } } } To assign value to the nested hash, we can use h[:data][:user][:value] = "Bob" However if any part in the middle is missing, it will cause error. Something like h.dig(:data, :user,…
sbs
  • 4,102
  • 5
  • 40
  • 54
25
votes
4 answers

Is there something like a Safe Navigation Operator that can be used on Arrays?

I have used Safe Navigation Operator for Objects to load on Asynchronous calls and it is pretty amazing. I thought I could reproduce the same for Arrays but it displays a template parse error in my Angular code. I know *ngIf is an alternative…
Sriram Jayaraman
  • 800
  • 1
  • 8
  • 15
21
votes
1 answer

Why does Ruby use its own syntax for safe navigation operator?

Ruby 2.3.0 introduces the safe navigation syntax that eases the nil handling of chained method calls by introducing a new operator that only calls the method if value of previous statement is not nil. This is a feature that already exists for…
Roope Hakulinen
  • 7,326
  • 4
  • 43
  • 66
5
votes
4 answers

Does Ruby safe navigation operator evaluate its parameters when its receiver is nil?

Question: Does Ruby safe navigation operator (&.) evaluate its parameters when its receiver is nil? For example: logger&.log("Something important happened...") Is the "Something important happened..." string evaluated here? Could you provide an…
Marian13
  • 7,740
  • 2
  • 47
  • 51
4
votes
2 answers

Access Observable in the Template without using the Safe Navigation Operator and Async Pipe Everytime

I'm new to angular and struggle a little with rxjs and all the async stuff going on. Some Context Lets assume you have a website like facebook with profiles. You can reach a profile by navigating to website.com/profiles/profileUrl. Equally when…
4
votes
2 answers

Why does the null-conditional operator change regular property access?

I'm confused about how the null-conditional operator cascades with normal property access. Take these two examples: a?.b.c (a?.b).c I would expect them to be equivalent: first, the value of a?.b is evaluated, then result.c is evaluated. Thus if a…
4
votes
2 answers

NilCheck fix on safe navigation operator (&.)

This simple method on a class just run the status method using the safe navigation operator. def current_status account&.status end But reek report this warning: MyClass#current_status performs a nil-check…
AndreDurao
  • 5,600
  • 7
  • 41
  • 61
3
votes
1 answer

Safe navigation operator for hash with string keys

I have a problem: data = { 'str_key' => ['string1', 'string2'] } # @param [Hash] data - hash with String key # @return [boolean] def some_logic_test?(data) data&..include?('string1') end How can I use the safe…
3
votes
2 answers

Ruby: Safe-navigation operator, undefined method `call`

I am trying to compare a number literal with the return value of a function that could return nil or a numeric. Consider this: def unreliable [nil, 42].sample end unreliable > 10 This will blow up 50% of the time with NoMethodError: undefined…
Ethan Kent
  • 381
  • 1
  • 4
  • 20
3
votes
2 answers

What is the pre-Ruby2.3 equivalent to the safe navigation operator (&. or "ampersand-dot")?

The answers to every question I can find (Q1, Q2) regarding Ruby's new safe navigation operator (&.) wrongly declare that obj&.foo is equivalent to obj && obj.foo. It's easy to demonstrate that this equivalence is incorrect: obj = false obj &&…
user513951
  • 12,445
  • 7
  • 65
  • 82
1
vote
0 answers

Returning result of statement including the safe navigation operator as boolean

I just started using the safe navigation operator in C# for the first time and I am wondering wether this is a correct use case for that operator: public bool HasAttributes { get { return this.SomeClassMember?.Attributes?.Count > 0; …
Chris
  • 1,417
  • 4
  • 21
  • 53
1
vote
1 answer

Where should I use Safe Navigation (Elvis Operator, the "?") in Angular?

We undoubtedly use safe navigation in string interpolation ({{}}) to safely navigate values in a nested object with checking nulls or undefined. I have a question that, is it okay to use safe navigation also for: ngIf ngFor ngModel ngClass and…
Faizan Saiyed
  • 804
  • 3
  • 17
  • 33
1
vote
0 answers

Is safe navigation operator have drawbacks on performance in Angular 2+

It's known that first ngOnChanges fires before bindings are initialized. So it's common to add safe navigation operator into expressions. @Component({ changeDetection: ChangeDetectionStrategy.OnPush, selector: 'some-component', …
Dzmitry Vasilevsky
  • 1,295
  • 2
  • 14
  • 25
1
2