safe-navigation-operator

Safe navigation operator with bracket property accesor

一世执手 提交于 2019-12-25 10:06:21
问题 I've run into a case where I can't use the dot notation to access a property, because the property's name contains a dot. I have an object called translations whose properties contain string translations, for example the Tooltip.O2 property contains the translation for the tooltip of an image: <img [matTooltip]="translations?.Tooltip.O2" [src]="bed.additionalO2 ? medO2 : noO2"> When I do this, it thinks I'm trying to access a Tooltip object inside translations with an O2 property. I'm aware

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

心已入冬 提交于 2019-12-18 12:14:39
问题 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, :value) = "Bob" won't work, since there's no Hash#dig= available yet. To safely assign value, we can do h.dig(:data, :user)&.[]=(:value, "Bob") # or equivalently h.dig(:data, :user)&.store(:value, "Bob") But is there better way to do that? 回答1: It's not without its

What is the pre-Ruby2.3 equivalent to the safe navigation operator (`&.`)?

社会主义新天地 提交于 2019-12-10 18:40:07
问题 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 && obj.foo # => false obj&.foo # => NoMethodError: undefined method `foo' for false:FalseClass Further, there is the problem of multiple evaluation. Replacing obj with an expression having side effects shows that the side effects are doubled only in the

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

て烟熏妆下的殇ゞ 提交于 2019-12-01 16:24:12
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 example in C#, Groovy and Swift. For example in Groovy , the syntax is foo?.bar which basically means that the result value is that of foo.bar unless foo is null , in which case the return value is also null and thus no exception is thrown. Also C# (called null-conditional operators ) and Swift (called optional-chaining expression ) use this notation. So

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

本小妞迷上赌 提交于 2019-12-01 15:57:22
问题 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 example in C#, Groovy and Swift. For example in Groovy, the syntax is foo?.bar which basically means that the result value is that of foo.bar unless foo is null , in which case the return value is also null and thus no exception is thrown. Also C#

Safe navigation operator (&.) for nil

点点圈 提交于 2019-11-27 06:35:51
问题 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 adding the lonely operator? 回答1: foo&.bar is shorthand for foo && foo.bar , so what would you expect the result of the expression nil && nil.nil? to be? 回答2: This is because nil&.nil? is shorthand for nil && nil.nil? . This would evaluate to nil && true ,

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

懵懂的女人 提交于 2019-11-26 12:10:51
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 simple example might look like this: def gender = user.male ? "male" : "female" //traditional ternary operator usage def displayName = user.name ?: "Anonymous" //more compact Elvis operator Safe Navigation Operator (?.) The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify

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

天大地大妈咪最大 提交于 2019-11-26 02:19:29
问题 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 simple example might look like this: def gender = user.male ? \"male\" : \"female\" //traditional ternary operator usage def displayName = user.name ?: \"Anonymous\" //more compact Elvis operator Safe Navigation Operator (?.) The Safe Navigation operator