https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112015-12-14T18:30:45ZRuby Issue Tracking SystemRuby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555352015-12-14T18:30:45Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>BTW, this came up in Matt Larraz's PR <a href="https://github.com/ruby/ruby/pull/1142" class="external">https://github.com/ruby/ruby/pull/1142</a></p>
<p>Both Hiroshi Shibata and Benoit Daloze thought (as I did) that <code>e_error_bytes&.dup.force_encoding(...)</code> should work.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555362015-12-14T23:00:43Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>I haven't thought it at all, but it seems interesting.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555372015-12-14T23:47:11Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>It seems very sensitive to determine how long it propagates.</p>
<p>How should we interpret <code>str&.upcase + "foo"</code>?</p>
<p>If it is considered as <code>str&.upcase&.+("foo")</code>, it may return nil (as I expect).<br>
If it is considered as <code>(str&.upcase) + "foo"</code>, it may raise an exception. We need write <code>p str&.upcase&.+("foo")</code>.</p>
<p>How's that for <code>str&.upcase == nil</code>?</p>
<p>If it is considered as <code>str&.upcase&.==(nil)</code>, it will return false or nil. Very confusing.<br>
If it is considered as <code>(str&.upcase) == nil</code>, it will return true or false.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@ruby-lang.org" class="email">mame@ruby-lang.org</a></p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555392015-12-15T03:49:30Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>If it propagates, we could write safe aref as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">ary</span><span class="o">&</span><span class="p">.</span><span class="nf">itself</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
<span class="n">ary</span><span class="o">&</span><span class="p">.</span><span class="nf">itself</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555402015-12-15T04:25:53Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Marc,</p>
<p>We need more clarification. Did you mean all method calling dots should be interpreted as <code>&.</code>?<br>
What about other calls like array/hash indexing or operators?</p>
<p>Matz.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555682015-12-15T18:01:05Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Glad to see we're headed towards having <code>x&.foo.bar</code> not be the same as <code>(x&.foo).bar</code>.</p>
<p>When we ask about <code>&.</code> vs operators, we can see that the question is similar to that of precedence, right? We could say that currently in trunk, <code>&.</code> has same "precedence" as <code>.</code>, which we now agree is not ideal. We need to bump <code>&.</code> down at least one level.</p>
<p>So either <code>&.</code> has a "precedence" just lower than <code>.</code>, or we (well, Matz) decide to make it even lower.</p>
<p>My guess is that the ideal would if <code>&.</code> has a "precedence" in between comparison operators (<, >=, ...) and equality operators (==, <=>, ...)</p>
<p>This way:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">bar</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">[</span><span class="mi">42</span><span class="p">]</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">[</span><span class="mi">42</span><span class="p">]</span> <span class="o">=</span> <span class="mi">43</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">*</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">+</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o"><<</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o"><</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">==</span> <span class="mi">42</span> <span class="c1"># => false ### This is where the precedence of &. is higher</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">||</span> <span class="mi">42</span> <span class="c1"># => 42</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="p">?</span> <span class="mi">1</span> <span class="p">:</span> <span class="mi">2</span> <span class="c1"># => 2</span>
</code></pre>
<p>My reasoning is that this divides the operators cleanly into those that we never want to apply to <code>nil</code> (e.g. <code>+</code>), from those that can be applied to it (e.g. <code>||</code>).</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555702015-12-15T18:45:22Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><blockquote>
<p>Did you mean all method calling dots should be interpreted as <code>&.</code>?</p>
</blockquote>
<p>Just to be clear, no, there should be a difference between <code>x&.foo&.bar</code> and <code>x&.foo.bar</code>. The following should be equivalent:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">bar</span>
<span class="c1"># same as</span>
<span class="k">if</span> <span class="n">temp</span> <span class="o">=</span> <span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span>
<span class="n">temp</span><span class="p">.</span><span class="nf">bar</span>
<span class="k">end</span>
</code></pre>
<p>In particular:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">=</span> <span class="no">Object</span><span class="p">.</span><span class="nf">new</span>
<span class="k">def</span> <span class="nc">x</span><span class="o">.</span><span class="nf">foo</span><span class="p">;</span> <span class="kp">nil</span><span class="p">;</span> <span class="k">end</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="o">&</span><span class="p">.</span><span class="nf">bar</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">bar</span> <span class="c1"># => NoMethodError, no method :bar for nil</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=555762015-12-16T00:38:19Zmlarraz (Matt Larraz)
<ul></ul><p>My naïve understanding is that <code>foo&.bar</code> should be a shorthand for <code>foo && foo.bar</code>, and therefore the <code>&.</code> operator should take the same level of precedence as the <code>&&</code> operator.</p>
<p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">bar</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">[</span><span class="mi">42</span><span class="p">]</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">[</span><span class="mi">42</span><span class="p">]</span> <span class="o">=</span> <span class="mi">43</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">*</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">+</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o"><<</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o"><</span> <span class="mi">42</span> <span class="c1"># => nil</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">==</span> <span class="mi">42</span> <span class="c1"># => false ### This is where the precedence of &. is higher</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">||</span> <span class="mi">42</span> <span class="c1"># => 42</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="p">?</span> <span class="mi">1</span> <span class="p">:</span> <span class="mi">2</span> <span class="c1"># => 2</span>
</code></pre>
</blockquote>
<p>If you substitute <code>x&.foo</code> for <code>x && x.foo</code> in the above, they all evaluate the same except for one:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span> <span class="o">&&</span> <span class="n">x</span><span class="p">.</span><span class="nf">foo</span> <span class="o">==</span> <span class="mi">42</span> <span class="c1"># => nil</span>
</code></pre>
<p>This makes sense, since <code>==</code> is a method and not an operator.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556062015-12-16T22:42:39Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Matz, did you get a chance to think about the "precedence" level of <code>&.</code>? It would be harder to change after christmas...</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556142015-12-17T03:14:05Zshugo (Shugo Maeda)
<ul></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Matz, did you get a chance to think about the "precedence" level of <code>&.</code>? It would be harder to change after christmas...</p>
</blockquote>
<p>Is it really hard to change after the release of Ruby 2.3?</p>
<p>IMHO, the current behavior of <code>x&.foo.bar</code> is useless, so users have to use <code>x&.foo&.bar</code> instead.<br>
Even if the behavior of <code>x&.foo.bar</code> is changed as you expect, <code>x&.foo&.bar</code> still works, and users<br>
can switch from <code>x&.foo&.bar</code> to <code>x&.foo.bar</code> gradually.</p>
<p>Please tell me if I miss anything.</p>
<p>I'm not against the proposal itself, but there's no enough time left....</p>
<p>FYI, the behavior of Groovy's <code>?.</code> seems to be the same as Ruby's <code>&.</code>:</p>
<pre><code>$ groovy -e 'x = null; print(x?.foo.bar)'
Caught: java.lang.NullPointerException: Cannot get property 'bar' on null object
java.lang.NullPointerException: Cannot get property 'bar' on null object
at script_from_command_line.run(script_from_command_line:1)
</code></pre>
<p>Please tell me if someone knows the behavior in C#.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556212015-12-17T08:36:43Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Shugo Maeda wrote:</p>
<blockquote>
<p>Is it really hard to change after the release of Ruby 2.3?</p>
</blockquote>
<p>True, it could be changed afterwards, although it is far from ideal.</p>
<blockquote>
<p>Please tell me if someone knows the behavior in C#.</p>
</blockquote>
<p>I don't know, but I can say that both CoffeeScript and Swift do the correct thing with respect to <code>x&.foo.bar</code>.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556222015-12-17T12:04:40Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Shugo Maeda wrote:</p>
<blockquote>
<p>Is it really hard to change after the release of Ruby 2.3?</p>
</blockquote>
<p>As Marc-Andre stated in <a href="https://bugs.ruby-lang.org/issues/11816#note-7" class="external">https://bugs.ruby-lang.org/issues/11816#note-7</a>, it will bring incompatibility. If we may change the spec in future, I think we should explicitly state the possibility in doc and release message. Also, it might be a good idea to mark the feature as "experimental".</p>
<hr>
<p>Another idea for the proposal: how about propagating <code>&.</code> as long as explicit method chain (that uses <code>.</code> literally) continues?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">*</span> <span class="mi">42</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">)</span> <span class="o">*</span> <span class="mi">42</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">*</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="o">&</span><span class="p">.</span><span class="nf">*</span><span class="p">(</span><span class="mi">42</span><span class="p">))</span> <span class="c1"># strictly not equivalent in the case where #foo returns nil</span>
</code></pre>
<p><code>x&.foo[42]</code> is still ambiguous. `(x&.foo)[42]' looks simple and consistent to me.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@ruby-lang.org" class="email">mame@ruby-lang.org</a></p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556242015-12-17T13:32:59Zshugo (Shugo Maeda)
<ul></ul><p>Yusuke Endoh wrote:</p>
<blockquote>
<blockquote>
<p>Is it really hard to change after the release of Ruby 2.3?</p>
</blockquote>
<p>As Marc-Andre stated in <a href="https://bugs.ruby-lang.org/issues/11816#note-7" class="external">https://bugs.ruby-lang.org/issues/11816#note-7</a>, it will bring incompatibility. If we may change the spec in future, I think we should explicitly state the possibility in doc and release message. Also, it might be a good idea to mark the feature as "experimental".</p>
</blockquote>
<p>It would be better if Matz would like to change the behavior in future.</p>
<blockquote>
<p>Another idea for the proposal: how about propagating <code>&.</code> as long as explicit method chain (that uses <code>.</code> literally) continues?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">*</span> <span class="mi">42</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">)</span> <span class="o">*</span> <span class="mi">42</span>
<span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="p">.</span><span class="nf">*</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span> <span class="o">==</span> <span class="p">(</span><span class="n">x</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span><span class="o">&</span><span class="p">.</span><span class="nf">*</span><span class="p">(</span><span class="mi">42</span><span class="p">))</span> <span class="c1"># strictly not equivalent in the case where #foo returns nil</span>
</code></pre>
</blockquote>
<p>In my understanding, rather than <code>(x&.foo&.*(42))</code>, <code>x&.foo.*(42)</code> is equivalent to <code>x && x.foo.*(42)</code> except when <code>x</code> is <code>false</code>.<br>
So, the phrase "propagating <code>&.</code>" is confusing, isn't it?</p>
<p>Anyway, your proposal sounds reasonable because <code>x&.foo * 42</code> is parsed as <code>(x && x.foo) * 42</code> rather than <code>x && (x.foo * 42)</code> in my brain.<br>
However, the behavior like <code>(x && x.foo) * 42</code> seems to be useless for the same reason as the current behavior of <code>x&.foo.bar</code>, so there is a trade-off.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556452015-12-18T04:06:08Zshugo (Shugo Maeda)
<ul></ul><p>Shugo Maeda wrote:</p>
<blockquote>
<p>Anyway, your proposal sounds reasonable because <code>x&.foo * 42</code> is parsed as <code>(x && x.foo) * 42</code> rather than <code>x && (x.foo * 42)</code> in my brain.</p>
</blockquote>
<p>Just for clarification, <code>x&.foo.bar</code> as <code>(x && x.foo).bar</code> looks more natural to me than <code>x && x.foo.bar</code>.</p>
<blockquote>
<p>However, the behavior like <code>(x && x.foo) * 42</code> seems to be useless for the same reason as the current behavior of <code>x&.foo.bar</code>, so there is a trade-off.</p>
</blockquote>
<p>So there is also a trade-off between the current behavior and Endoh-san's proposal.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556462015-12-18T04:27:23Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>I understand this as the idea returned value of safe navigation operator behaves like NaN while the method chain.<br>
It sounds interesting and feels bit useful but I weakly object it.</p>
<p>Anyway using && in &.'s explanation should be harmful especially this serious topic because the behavior is different in handling false.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=556912015-12-20T05:14:49Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Matz, is it possible to have a decision before the official release, so we at least know what the future might bring and can introduce it properly?</p>
<p>I still hope my "whichever is the most useful" proposal is accepted. We look at it right now with a mental magnifying glass, but I think that it would become second nature in practice, as I feel it would usually be what the rubyist wants. To me, it feels similar to <code>super</code> which passes argument and block; not necessarily intuitive when you think about it, but if you don't think about it, it does usually what you want to do.</p>
<p>Thanks</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=561802016-01-20T02:36:07Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>Dear Matz,</p>
<p>Any update on the precedence of <code>&.</code>?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=561922016-01-20T10:01:39Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Marc,</p>
<p>I don't feel it's right to ignore nils in calls after <code>&.</code><br>
Nil-ignorance should be explicit, I think.</p>
<p>Matz.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=562132016-01-20T18:55:50Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>So if I understand correctly, you like the current behavior, although it is not useful in any circumstance I can think of?</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">e_error_bytes</span><span class="o">&</span><span class="p">.</span><span class="nf">dup</span><span class="p">.</span><span class="nf">force_encoding</span><span class="p">(</span><span class="o">...</span><span class="p">)</span> <span class="c1">#=> NoMethodError if error_bytes is nil</span>
</code></pre>
<p>So there are basically no circumstances where one would write <code>foo&.bar</code> and follow it with <code>.</code>, <code><</code>, <code>==</code>, etc</p>
<p>Even Swift allows <code>x&.foo.bar</code> to be something meaningful.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=571192016-02-24T17:52:44Zseanlinsley (Sean Linsley)
<ul></ul><p>I prefer Marc's proposal here: <a href="https://bugs.ruby-lang.org/issues/11816#note-6" class="external">https://bugs.ruby-lang.org/issues/11816#note-6</a>. I think that's the much more natural than the existing behavior. I found this ticket after being surprised by the behavior, as I attempted to update my project to use <code>&.</code>. Most of the places I would want to use <code>&.</code> currently have an if condition wrapping them, because the first couple objects in the chain may not exist. With the feature as it is currently, it's pretty much useless, because this is unacceptably hard to read:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">book</span><span class="o">&</span><span class="p">.</span><span class="nf">authors</span><span class="o">&</span><span class="p">.</span><span class="nf">last</span><span class="o">&</span><span class="p">.</span><span class="nf">first_name</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=575182016-03-17T06:34:06Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>I was not active when this topic was hot, so to know the current status I asked this on this month's developer meeting. The bottom line is there was no concrete answer for this issue yet.</p>
<p>At first "propagating &. as long as explicit method chain continues" seemed to be the right way. But we found there still are edge cases that should be clearly described before adopting that way. Consider:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">+=</span> <span class="n">d</span>
</code></pre>
<p>here, if it was <code>a.b.c += d</code>, it calls two methods <code>a.b.c</code> and <code>a.b.c=</code>. Then what should happen for <code>&.</code> ?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=575562016-03-17T18:17:23Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>I feel that <code>a&.b.c += d</code> is still an assignment, so should be treated with the same precedence.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">+=</span> <span class="n">d</span> <span class="c1"># => nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">||=</span> <span class="n">d</span> <span class="c1"># => nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">&&=</span> <span class="n">d</span> <span class="c1"># => nil</span>
<span class="c1"># etc...</span>
</code></pre>
<p>I think this is the most helpful solution.</p>
<p>Moreover, we already have (in 2.3.0)</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span> <span class="o">+=</span> <span class="n">d</span> <span class="c1"># => nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span> <span class="o">||=</span> <span class="n">d</span> <span class="c1"># => nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span> <span class="o">&&=</span> <span class="n">d</span> <span class="c1"># => nil</span>
</code></pre>
<p>Given this, I feel the resolution I'm giving is also the most intuitive.</p>
<p>Just to be sure, with "propagating &. as long as explicit method chain continues", we consider that <code>x&.foo * 42</code> returns <code>nil</code>, as calling <code>*</code> is equivalent to <code>.*</code>, right?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=575682016-03-18T06:18:33Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Marc-Andre Lafortune wrote:</p>
<blockquote>
<p>Just to be sure, with "propagating &. as long as explicit method chain continues", we consider that <code>x&.foo * 42</code> returns <code>nil</code>, as calling <code>*</code> is equivalent to <code>.*</code>, right?</p>
</blockquote>
<p>I think so.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=579842016-04-08T11:42:56Zsawa (Tsuyoshi Sawada)
<ul></ul><p>What about allowing parentheses after the safe navigation operator like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="o">&.</span><span class="p">(</span><span class="n">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">)</span>
</code></pre>
<p>which should behave in the way proposed.</p>
<p>The edge cases:</p>
<p>Shyouhei Urabe wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">+=</span> <span class="n">d</span>
</code></pre>
</blockquote>
<p>with the two possibilities involved can be distinguished as:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="o">&.</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="nf">c</span><span class="p">)</span> <span class="o">+=</span> <span class="n">d</span> <span class="c1"># non-useful one</span>
<span class="n">a</span><span class="o">&.</span><span class="p">(</span><span class="n">b</span><span class="p">.</span><span class="nf">c</span> <span class="o">+=</span> <span class="n">d</span><span class="p">)</span>
</code></pre>
</blockquote>
<p>Does this conflict with the current syntax?</p>
<p>Or, if <code>bar</code> above is confusing (to human) with a local variable, then another option may be:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="o">&</span><span class="p">(.</span><span class="nf">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">)</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=579852016-04-08T12:44:44Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/57985/diff?detail_id=40839">diff</a>)</li></ul><p>Tsuyoshi Sawada wrote:</p>
<blockquote>
<p>Does this conflict with the current syntax?</p>
</blockquote>
<p>Yes.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">proc</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span><span class="nb">p</span> <span class="n">x</span><span class="p">}</span><span class="o">&.</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1">#=> 1</span>
</code></pre>
<blockquote>
<p>Or, if <code>bar</code> above is confusing (to human) with a local variable, then another option may be:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="o">&</span><span class="p">(.</span><span class="nf">bar</span><span class="p">.</span><span class="nf">baz</span><span class="p">)</span>
</code></pre>
</blockquote>
<p>Is <code>&(.</code> a single token?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581172016-04-18T00:36:56Zjoanbm (Joan Blackmoore)
<ul></ul><p>I can understand the intention behind the proposal, but would like to express a (strong) disagreement with it.</p>
<p>The thing is that it would break language's consistency and is in contradiction to declared behavior.</p>
<p>"safe navigation operator" serves, by accepted feature request <a href="https://bugs.ruby-lang.org/issues/11537" class="external">#11537</a>, as an alternate way of method invocation. The only difference to standard method sending is ignoring passed argument if target object is <em>nil</em>. This means, expression may result either in a value of expected type or <em>nil</em> and programmer need count with this eventuality.</p>
<p>This bug report, or rather feature request, asks for change of existing role of <em>&.</em> as a "safe" methods chain navigator and make it later/last resort objects combinator.<br>
Lowering operator's precedence would result in much harder to understand code, in figuring out at which moment it would be applied and what would be its argument. In comparison to unambiguous situation, when it has the highest implicit precedence, like the standard dot method call.</p>
<p>Setting an explicit precedence by parens would remedy the situation but, as already mentioned by Nobu, it would clash with existing <em>.()</em> syntax, a very unfortunate design decision introduced into the language as a parse-time way of writing Proc/Method#call . Confusing with standard method arguments passing, enforcing eval precedence, optional dot at operator methods… Unless widely used, its removal in Ruby 3.0 would be highly welcomed.</p>
<p>I'd suggest keep the existing behavior or possibly extend with <code>#respond_to?</code> method check and get rid of exception to the rule when applied to <em>NillClass</em> instance, like <code>nil&.nil?</code> resulting in <code>true</code>.</p>
<p>It may also be taken into the consideration a little inconsistency when followed by operator method:</p>
<p>optional dot omitted<br>
<code>40+2 # 42</code><br>
<code>40&.+2 # 42</code></p>
<p>vs</p>
<p><code>40.+2 # 42</code><br>
<code>40&..+2 # syntax error</code></p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581182016-04-18T01:11:05Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>I don't necessarily disagree with the rest of Joan's post, but for this point:</p>
<p>Joan Blackmoore wrote:</p>
<blockquote>
<p>It may also be taken into the consideration a little inconsistency when followed by operator method:</p>
<p>optional dot omitted<br>
<code>40+2 # 42</code><br>
<code>40&.+2 # 42</code></p>
<p>vs</p>
<p><code>40.+2 # 42</code><br>
<code>40&..+2 # syntax error</code></p>
</blockquote>
<p>That's perhaps not the right consistency to be searching for; the <code>&.</code> operator is a dressed up <code>.</code> operator. There's no equivalent sugar-coated safe operator syntax.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">40</span> <span class="o">+</span> <span class="mi">2</span> <span class="c1"># "unsafe"</span>
<span class="c1">#??? # "safe"</span>
<span class="mi">40</span><span class="p">.</span><span class="nf">+</span><span class="mi">2</span> <span class="c1"># "unsafe"</span>
<span class="mi">40</span><span class="o">&</span><span class="p">.</span><span class="nf">+</span><span class="mi">2</span> <span class="c1"># "safe"</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581202016-04-18T05:34:00Zjoanbm (Joan Blackmoore)
<ul></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/4017">@matthew (Matthew Johnson)</a></p>
<p>Thought about it again and would agree with the last paragraph. Direct substitution is not appropriate here, despite it sounds logical.<br>
The <em>&.</em> operator is a strange beast as other general rules also won't apply, like (optional) preceding dot, ie. <em>object.&.hash</em> is also a syntax error.</p>
<p>Btw. by playing with different &. contained expressions, I've discovered a possible bug:</p>
<p>This runs ok</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="kp">nil</span>
<span class="n">a</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">&&=</span> <span class="kp">false</span> <span class="c1"># nil</span>
</code></pre>
<p>Following however freezes VM and need to be SIGKILLed</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">nil</span><span class="o">&</span><span class="p">.</span><span class="nf">foo</span> <span class="o">&&=</span> <span class="kp">false</span>
</code></pre>
<p>Even at bytecode compilation</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">RubyVM</span><span class="o">::</span><span class="no">InstructionSequence</span><span class="p">.</span><span class="nf">compile</span><span class="p">(</span><span class="s1">'nil&.foo ||= 42'</span><span class="p">)</span>
</code></pre>
<p>tail of strace output</p>
<pre><code>lstat("/usr", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib64", {st_mode=S_IFDIR|0755, st_size=139264, ...}) = 0
lstat("/usr/lib64/ruby", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib64/ruby/2.3.0", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/usr/lib64/ruby/2.3.0/unicode_normalize.rb", {st_mode=S_IFREG|0644, st_size=3265, ...}) = 0
</code></pre>
<p>Tested with</p>
<pre><code>ruby -v
ruby 2.3.0p75 (2016-04-07 revision 54505) [x86_64-linux]
</code></pre>
<p>command used</p>
<pre><code class="sh syntaxhl" data-language="sh">strace ruby <span class="nt">--disable</span><span class="o">=</span>gems,rubyopt <span class="nt">-e</span> <span class="s1">'nil&.foo &&= false'</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581212016-04-18T07:07:09Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset r54628.</p>
<hr>
<p>compile.c: fix dangling link</p>
<ul>
<li>compile.c (iseq_peephole_optimize): should not replace the<br>
current target INSN, not to follow the replaced dangling link in<br>
the caller. <a href="/issues/11816">[ruby-core:74993]</a> [Bug <a class="issue tracker-2 status-2 priority-4 priority-default" title="Feature: Partial safe navigation operator (Assigned)" href="https://redmine.ruby-lang.org/issues/11816">#11816</a>]</li>
</ul> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581222016-04-18T07:13:42Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Open</i></li><li><strong>Backport</strong> changed from <i>2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN</i> to <i>2.1: DONTNEED, 2.2: DONTNEED, 2.3: UNKNOWN</i></li></ul> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581282016-04-18T08:28:02Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Precedes</strong> <i><a class="issue tracker-1 status-5 priority-4 priority-default closed" href="/issues/12296">Bug #12296</a>: [Backport] fix dangling link</i> added</li></ul> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581322016-04-18T08:53:27Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul><p>Applied in changeset ruby_2_3|r54635.</p>
<hr>
<p>merge revision(s) 54628: [Backport <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: [Backport] fix dangling link (Closed)" href="https://redmine.ruby-lang.org/issues/12296">#12296</a>]</p>
<pre><code>* compile.c (iseq_peephole_optimize): should not replace the
current target INSN, not to follow the replaced dangling link in
the caller. <a href="/issues/11816">[ruby-core:74993]</a> [Bug #11816]
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581412016-04-18T16:38:02Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Closed</i> to <i>Assigned</i></li></ul> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=581432016-04-18T17:14:54Zjoanbm (Joan Blackmoore)
<ul></ul><p>Oh, thanks for the fix. That was really fast.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=679552017-11-28T04:34:38Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>ruby -v</strong> deleted (<del><i>preview 2</i></del>)</li><li><strong>Backport</strong> deleted (<del><i>2.1: DONTNEED, 2.2: DONTNEED, 2.3: UNKNOWN</i></del>)</li></ul><p>Matz, did you get a chance to consider the precedence of <code>&.</code>?</p>
<p>As a reminder, there's currently no real use for <code>foo&.bar.baz</code> or similar. We are forced to write <code>foo&.bar&.baz</code> even though this could introduce unwanted errors, e.g. if <code>bar</code> was erroneously returning <code>nil</code>.</p>
<p>I still believe that my proposal <a href="https://bugs.ruby-lang.org/issues/11816#note-6" class="external">https://bugs.ruby-lang.org/issues/11816#note-6</a> is the right one.</p>
<p>Thanks for the consideration.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=681432017-12-02T08:47:09Zznz (Kazuhiro NISHIYAMA)
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>As a reminder, there's currently no real use for <code>foo&.bar.baz</code> or similar. We are forced to write <code>foo&.bar&.baz</code> even though this could introduce unwanted errors, e.g. if <code>bar</code> was erroneously returning <code>nil</code>.</p>
</blockquote>
<p>I found real use case in <a href="https://qiita.com/jnchito/items/6161647b624fb51cb815" class="external">【アンチパターン】全部nil(null)かもしれない症候群</a>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">friend</span><span class="o">&</span><span class="p">.</span><span class="nf">message</span><span class="p">.</span><span class="nf">blank?</span>
</code></pre> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=681832017-12-04T11:07:53Zsawa (Tsuyoshi Sawada)
<ul></ul><p>znz (Kazuhiro NISHIYAMA) wrote:</p>
<blockquote>
<p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>As a reminder, there's currently no real use for <code>foo&.bar.baz</code> or similar. We are forced to write <code>foo&.bar&.baz</code> even though this could introduce unwanted errors, e.g. if <code>bar</code> was erroneously returning <code>nil</code>.</p>
</blockquote>
<p>I found real use case in <a href="https://qiita.com/jnchito/items/6161647b624fb51cb815" class="external">【アンチパターン】全部nil(null)かもしれない症候群</a>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">friend</span><span class="o">&</span><span class="p">.</span><span class="nf">message</span><span class="p">.</span><span class="nf">blank?</span>
</code></pre>
</blockquote>
<p>I don't think that is a real use case. The linked article mentions coding style. It suggests not to use the safe navigation operator without thinking or when it is not necessary. The feature proposed in this thread actually makes these things happen automatically, and will remove the necessity of such consideration. It is actually consistent with the current proposal.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=681872017-12-04T12:28:36Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>sawa (Tsuyoshi Sawada) wrote:</p>
<blockquote>
<p>I don't think that is a real use case. The linked article mentions coding style. It suggests not to use the safe navigation operator without thinking or when it is not necessary. The feature proposed in this thread actually make these things happen automatically, and will remove the necessity of such consideration. It is actually consistent with the current proposal.</p>
</blockquote>
<p>I think <code>.nil?</code>, <code>.inspect</code>, <code>.tap</code> could all be legitimately used on a value or nil.</p>
<p>It makes sense to me that <code>&.</code> is just a safe version of <code>.</code>, not some sort of "infectious nil" operation that propagates until an arbitrary precedence limit is hit. If there was a way to explicitly signal the end of the "infectious nil" I'd probably find it useful (for example, a nicer version of <code>x&.instance_eval{foo.bar}</code>), but then I don't think that's <code>&.</code>; it's something new.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=681892017-12-04T17:43:50Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>If there was a way to explicitly signal the end of the "infectious nil" I'd probably find it useful</p>
</blockquote>
<p>There is one way, and it is the same as with all the cases where the precedence doesn't go the way you want it: parentheses.</p>
<pre><code>(foo || bar) && baz
(friend&.message).blank?
</code></pre>
<p>The whole point of precedence is to allow writing things simply and without parentheses most of the time.</p>
<p>Matz: was this discussed at the developers' meeting?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=682032017-12-06T04:06:20Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>marcandre (Marc-Andre Lafortune) wrote:</p>
<blockquote>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>If there was a way to explicitly signal the end of the "infectious nil" I'd probably find it useful</p>
</blockquote>
<p>There is one way, and it is the same as with all the cases where the precedence doesn't go the way you want it: parentheses.</p>
<pre><code>(foo || bar) && baz
(friend&.message).blank?
</code></pre>
<p>The whole point of precedence is to allow writing things simply and without parentheses most of the time.</p>
</blockquote>
<p>Hmm, that's <em>almost</em> right, but in my mind it still doesn't quite fit. I think the problem I have is the expectation that modifying one message dispatch (<code>.</code> → <code>&.</code>) shouldn't affect subsequent messages. If you want to affect the dispatch of a group of messages you should use a scoping construct, and operator precedence (even with parens) isn't <em>scope</em>. That's why <code>foo&.instance_eval{bar.baz}</code> feels right, even if it's ugly.</p>
<p>Perhaps I am alone in seeing <code>&.</code> as an armoured <code>.</code>, not an executing <code>&&</code>. (Despite the fact that <code>false&.! == true</code>)</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=850662020-04-12T03:20:01Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>FWIW, EcmaScript 2020 introduced a similar operator, see <a href="https://github.com/tc39/proposal-optional-chaining/" class="external">https://github.com/tc39/proposal-optional-chaining/</a><br>
. The precedence is between <code>[]</code> and <code>*</code>, higher than I propose in <a href="https://bugs.ruby-lang.org/issues/11816#note-6" class="external">https://bugs.ruby-lang.org/issues/11816#note-6</a> but at least low enough to be actually useful.</p>
<p>Matz, any chance we'll have a more useful operator precedence in Ruby 3?</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=850922020-04-13T17:29:39ZDan0042 (Daniel DeLorme)
<ul></ul><p>The <code>&.</code> operator has pretty well-defined semantics by now and changing it <em>may</em> break some existing code. Maybe a possible alternative would be a similar but separate operator like <code>&&.</code> / <code>and.</code> which would then intuitively have the same precedence as <code>&&</code> / <code>and</code>.</p>
<p><code>x and .foo.bar</code> reads quite well to me, better than <code>x&.foo.bar</code></p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=850972020-04-14T03:49:27Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><blockquote>
<p>The &. operator has pretty well-defined semantics by now and changing it may break some existing code.</p>
</blockquote>
<p>Any change may break some existing code. Given the fact that <code>foo&.bar.baz</code> have basically no use whatsover, it is difficult to see what functioning code could be broken though. Let's also think of how many potential bugs we are creating by forcing Rubyists to write <code>foo&.bar&.baz</code> when that's not what they mean.</p> Ruby master - Feature #11816: Partial safe navigation operatorhttps://redmine.ruby-lang.org/issues/11816?journal_id=850982020-04-14T08:02:50Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><blockquote>
<p>Given the fact that <code>foo&.bar.baz</code> have basically no use whatsover, it is difficult to see what functioning code could be broken though</p>
</blockquote>
<p>I can imagine some!</p>
<ul>
<li>Not from production, but it does not seem too obscure: <code>foo&.bar.nil?</code> would be <code>true</code> in the current implementation, and <code>nil</code> in "skip-the-rest" implementation.</li>
<li>A bit more obscure, but this one is from real code: <code>find.some.array&.first.tap { |val| log.debug "nothing found" if val.nil? }</code>
</li>
<li>... or <code>find_key_vaue_pair(hash)&.last.then { |val| val.nil? ? default : val }</code> (not just <code>||</code>, because <code>false</code> is acceptable value)</li>
</ul>
<p>Obviously, all can be rewritten another way -- but obviously, all will be currently broken by the change proposed.</p>