https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?15582348732017-08-16T12:44:42ZRuby Issue Tracking SystemRuby trunk - Feature #13820: Add a nill coalescing operatorhttps://bugs.ruby-lang.org/issues/13820?journal_id=662032017-08-16T12:44:42Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I am not sure that using a special-purpose operator would make a lot of sense.</p>
<p>I myself use nil primarily as means to indicate a default, &quot;non-set&quot; value. The<br>
moment it is set to a boolean, be it false or true, is for me an indication<br>
that it has been &quot;upgraded&quot; (or set by a user on the commandline etc...)</p>
<p>I do also tend to explicitely query for .nil? on some objects.</p>
<p>By the way, did you actually propose an actual syntax? The two &#39;?&#39;?</p>
<p>I do not think that ?? has any realistic chance for implementation due to<br>
? already being used in ruby - in method definitions or ternary<br>
operator for example. People may wonder why there are so many ? coming out<br>
of nowhere. (For the record, I also consider || to be not pretty ... I <br>
strangely end up using a more verbose but explicit way to set or ensure<br>
defaults in ruby code. I would never write a line such as &quot;<br>
port = opts[:port] || (https ? 443 : 80)&quot; simply because it takes my<br>
brain too long to process what is going on there; my code always ends<br>
up being so simple that I do not have to think about it much at all).</p>
Ruby trunk - Feature #13820: Add a nill coalescing operatorhttps://bugs.ruby-lang.org/issues/13820?journal_id=662072017-08-16T21:56:08Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>In perl I find <code>$x // $y</code> useful vs <code>$x || $y</code> because sometimes you want to accept <code>&quot;&quot;</code> and <code>0</code> as values.</p>
<p>In ruby the only &#39;defined&#39; falsey value is <code>false</code>, so I&#39;m not sure how useful this feature is here.</p>
<p>If you&#39;re pulling options from a hash, for example, it&#39;s probably better to use a signal like <code>h.fetch &#39;x&#39;, y</code> to show that you accept falsey values from the hash, and/or <code>x.nil? ? y : x</code> to show that you explicitly only don&#39;t want <code>nil</code></p>
Ruby trunk - Feature #13820: Add a nill coalescing operatorhttps://bugs.ruby-lang.org/issues/13820?journal_id=662092017-08-16T23:45:23Zwilliamn (William Newbery)
<ul></ul><p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>By the way, did you actually propose an actual syntax? The two &#39;?&#39;?</p>
</blockquote>
<p>Not really personally set on any given syntax, just <code>??</code> and <code>//</code> are familiar to me from other programming. Although actually for <code>??</code> specifically, I guess the fact Ruby uses it in both methods and ternary causes a conflict rather than just one or the other (<code>x.nil?? &quot;was nil&quot; : &quot;not nil&quot;</code>). I wouldn&#39;t know if the parser can figure that out or not.</p>
<p>But more the concept that any specific syntax.</p>
<blockquote>
<p>I myself use nil primarily as means to indicate a default, &quot;non-set&quot; value. The<br>
moment it is set to a boolean, be it false or true, is for me an indication<br>
that it has been &quot;upgraded&quot; (or set by a user on the commandline etc...)</p>
</blockquote>
<p>Hmm, maybe I didn&#39;t explain clearly. That is pretty much the pattern I come across repeatedly in Ruby code, and it fails for the <code>false</code> value because false is not &quot;upgraded&quot; when people do &quot;x || my_default&quot;.</p>
<pre><code class="ruby syntaxhl"><span class="s2">"a truthy value"</span> <span class="o">||</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># The operator also short circuits so the method `foo` will never even get called</span>
<span class="kp">false</span> <span class="o">||</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># Left side is falsy, so evaluate the right side, but is often not the intent</span>
<span class="kp">nil</span> <span class="o">||</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># Nil is also falsy, so evaluate the right side</span>
<span class="s2">"a truthy value"</span> <span class="p">??</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># String is true and not nil, so nothing changes here</span>
<span class="kp">false</span> <span class="sc">??</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># This changes. Left side is not nil, so the right side is never evaluated</span>
<span class="kp">nil</span> <span class="sc">??</span> <span class="n">foo</span><span class="p">(</span><span class="s2">"something else"</span><span class="p">)</span> <span class="c1"># Like with `||`, left side is nil so evaluate the right side</span>
<span class="c1"># so this "does the right thing", as far as my maybe not great example goes</span>
<span class="n">https</span> <span class="o">=</span> <span class="n">opts</span><span class="p">[:</span><span class="n">https</span><span class="p">]</span> <span class="p">??</span> <span class="kp">true</span>
</code></pre>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>In perl I find <code>$x // $y</code> useful vs <code>$x || $y</code> because sometimes you want to accept <code>&quot;&quot;</code> and <code>0</code> as values.<br>
But not <code>false</code>?</p>
</blockquote>
<p>While <code>hash.fetch</code> is nice, I still see <code>||</code> used a lot, in places that maybe wont convert so nice. Also it wont short circuit, in the event the default is not trivial (e.g. with say active record stuff, its easy to have something that goes to the DB without really thinking about it).</p>
<pre><code class="ruby syntaxhl"><span class="n">opts</span><span class="p">[</span><span class="ss">:foo</span><span class="p">]</span> <span class="o">||</span> <span class="vi">@foo_config</span> <span class="o">||</span> <span class="no">App</span><span class="p">.</span><span class="nf">config</span><span class="p">.</span><span class="nf">foo</span> <span class="c1"># Occasionally I see 3 or more chained together</span>
<span class="nb">hash</span><span class="p">[</span><span class="ss">:foo</span><span class="p">]</span> <span class="o">||=</span> <span class="n">fetch_foo</span> <span class="c1"># fetch doesn't assign the value like this does, and it wont short circuit `fetch_foo`</span>
<span class="vi">@lazy</span> <span class="o">||=</span> <span class="n">calc_lazy</span> <span class="c1"># Sometimes used with non-hashes</span>
</code></pre>
<p>But yes your right, they can all be done other ways, and maybe the better answer is to discourage <code>||</code> in the first place, but I struggled finding ones as tidy to suggest instead.</p>
<pre><code class="ruby syntaxhl"><span class="n">opts</span><span class="p">.</span><span class="nf">fetch</span><span class="p">(</span><span class="ss">:foo</span><span class="p">,</span> <span class="o">!</span><span class="vi">@foo_config</span><span class="p">.</span><span class="nf">nil?</span> <span class="vi">@foo_config</span> <span class="p">:</span> <span class="no">App</span><span class="p">.</span><span class="nf">config</span><span class="p">.</span><span class="nf">foo</span><span class="p">)</span>
<span class="nb">hash</span><span class="p">[</span><span class="ss">:foo</span><span class="p">]</span> <span class="o">=</span> <span class="n">fetch_foo</span> <span class="k">if</span> <span class="o">!</span><span class="nb">hash</span><span class="p">.</span><span class="nf">has_key?</span><span class="p">(</span><span class="ss">:foo</span><span class="p">)</span>
<span class="vi">@lazy</span> <span class="o">=</span> <span class="n">calc_lazy</span> <span class="k">if</span> <span class="vi">@lazy</span><span class="p">.</span><span class="nf">nil?</span>
<span class="vi">@lazy</span> <span class="c1"># needed because get nil if the if condition is false</span>
<span class="k">if</span> <span class="vi">@lazy</span><span class="p">.</span><span class="nf">nil?</span>
<span class="vi">@lazy</span> <span class="o">=</span> <span class="n">calc_lazy</span>
<span class="k">end</span>
<span class="vi">@lazy</span>
</code></pre> Ruby trunk - Feature #13820: Add a nill coalescing operatorhttps://bugs.ruby-lang.org/issues/13820?journal_id=662102017-08-17T00:01:23Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>williamn (William Newbery) wrote:</p>
<blockquote>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>In perl I find <code>$x // $y</code> useful vs <code>$x || $y</code> because sometimes you want to accept <code>&quot;&quot;</code> and <code>0</code> as values.</p>
</blockquote>
<p>But not <code>false</code>?</p>
</blockquote>
<p>Not in perl ;)</p>
<blockquote>
<p>While <code>hash.fetch</code> is nice, I still see <code>||</code> used a lot, in places that maybe wont convert so nice. Also it wont short circuit, in the event the default is not trivial (e.g. with say active record stuff, its easy to have something that goes to the DB without really thinking about it).</p>
</blockquote>
<p>Yes, short-circuit is handy. It&#39;s why I was a proponent of <code>&amp;.</code>. Maybe it&#39;s okay to add <code>//</code> even if it&#39;s only used sometimes.</p>
Ruby trunk - Feature #13820: Add a nill coalescing operatorhttps://bugs.ruby-lang.org/issues/13820?journal_id=662242017-08-18T00:16:34Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>williamn (William Newbery) wrote:</p>
<blockquote>
<p>shevegen (Robert A. Heiler) wrote:</p>
<blockquote>
<p>By the way, did you actually propose an actual syntax? The two &#39;?&#39;?</p>
</blockquote>
<p>Not really personally set on any given syntax, just <code>??</code> and <code>//</code> are familiar to me from other programming. Although actually for <code>??</code> specifically, I guess the fact Ruby uses it in both methods and ternary causes a conflict rather than just one or the other (<code>x.nil?? &quot;was nil&quot; : &quot;not nil&quot;</code>). I wouldn&#39;t know if the parser can figure that out or not.</p>
</blockquote>
<p><code>??</code> is a string literal, and <code>//</code> is a regexp literal.</p>
<blockquote>
<pre><code class="ruby syntaxhl"><span class="k">def</span> <span class="nf">fetch</span><span class="p">(</span><span class="nb">id</span><span class="p">,</span> <span class="o">**</span><span class="n">opts</span><span class="p">)</span>
<span class="n">host</span> <span class="o">=</span> <span class="n">opts</span><span class="p">[</span><span class="ss">:host</span><span class="p">]</span> <span class="o">||</span> <span class="n">default_host</span>
<span class="n">https</span> <span class="o">=</span> <span class="n">opts</span><span class="p">[</span><span class="ss">:https</span><span class="p">]</span> <span class="o">||</span> <span class="kp">true</span>
<span class="n">port</span> <span class="o">=</span> <span class="n">opts</span><span class="p">[</span><span class="ss">:port</span><span class="p">]</span> <span class="o">||</span> <span class="p">(</span><span class="n">https</span> <span class="p">?</span> <span class="mi">443</span> <span class="p">:</span> <span class="mi">80</span><span class="p">)</span>
</code></pre></blockquote>
<p>Why not keyword arguments?</p>