From: "genezys (Vincent Robert) via ruby-core" Date: 2024-11-26T09:41:49+00:00 Subject: [ruby-core:120011] [Ruby master Feature#13820] Add a nil coalescing operator Issue #13820 has been updated by genezys (Vincent Robert). Here are my 2 cents regarding this subject, if it can help moving forward with this feature. Wkipedia has a great article explaining and listing null coalescing operators in other languages: https://en.wikipedia.org/wiki/Null_coalescing_operator Here is a summary of all operators ordered by usage: - `??`: ATS, C#, JavaScript, PHP, PowerShell, Swift - No operator: Python, Ruby, Rust, SQL - `?:`: CFML, Kotlin, Objective-C - `!`: Freemarker, Haskell - `:-`: Bourne-like shells - `|?`: F# - `//`: Perl - `%||%`: R - `If`: VB.NET All languages that do not provide an operator offer a way to perform null coalescing. In Ruby, you could add a `coalesce` method with a special implementation on NilClass: ``` class Object def coalesce(value = nil) self end end class NilClass def coalesce(value = nil) block_given? ? yield : value end end > "a".coalesce("b") # => "a" > "a".coalesce { "b" } # => "a" > nil.coalesce("b") # => "b" > nil.coalesce { "b" } # => "b" > false.coalesce(true) # => false > nil.coalesce(true) # => true ``` Keep in mind that this does not address the issue of the null coalescing assignment operator (aka `??=`) ---------------------------------------- Feature #13820: Add a nil coalescing operator https://bugs.ruby-lang.org/issues/13820#change-110753 * Author: williamn (William Newbery) * Status: Open ---------------------------------------- It would be nice if Ruby had an operator that only considered `nil` as false, like the null coalescing operators or "Logical Defined-Or operator" (Perl) found in some other languages. Ive seen things like `//` and `//=`m `??` and `??=`, or `?:` used for this. This would work like `||` and `||=` for short circuiting etc. except that only `nil` is considered a false condition. While Ruby considers only "false" and "nil" as false, with everything else true ("", [], {}, etc.) I still find occasionally people trip up when using logical or, `||` and `||=` when the value may be false. ```ruby a = 0 || 55 # = 0 Ruby already considers 0, "", etc. as true (oter languages do differ a lot here) a = 0 ?? 55 # = 0 So no change here a = nil || 55 # = 55, nil is false so right side is evaulated. a = nil ?? 55 # = 55, again no change a = false || 55 # = 55, however false is false for logical or a = false ?? 55 # = false, but its still a non-nil value ``` For example when doing things like: ```ruby def lazy @lazy ||= compute_this end def fetch(id, **opts) host = opts[:host] || default_host https = opts[:https] || true port = opts[:port] || (https ? 443 : 80) ... ``` Normally the intention is to use a default value or compute an action if no value is provided, which if the value may be false then requires special handling, or sometimes is missed and results in a bug. -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/