https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112015-12-13T11:42:31ZRuby Issue Tracking SystemRuby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=555142015-12-13T11:42:31Zyugui (Yuki Sonoda)yugui@yugui.jp
<ul><li><strong>Assignee</strong> set to <i>matz (Yukihiro Matsumoto)</i></li></ul> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=555162015-12-13T18:17:37Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul></ul><p>IMO, we can write <code>&.</code> only for replacement of <code>.</code>.<br>
As you know, <code>ary.[idx]</code> is not valid, then <code>ary&.[idx]</code> should not be valid, too.</p> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=555182015-12-14T05:04:48Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Usaku NAKAMURA wrote:</p>
<blockquote>
<p>IMO, we can write <code>&.</code> only for replacement of <code>.</code>.<br>
As you know, <code>ary.[idx]</code> is not valid, then <code>ary&.[idx]</code> should not be valid, too.</p>
</blockquote>
<p>That is same as matz's opinion and the reason it was removed at r52430.</p>
<pre><code>parse.y: revert lbracket
* parse.y (lbracket): remove .? before aref. [Feature #11537]
revert r52422 and r52424
</code></pre>
<p>I don't think this proposal will be accepted.<br>
We'll need a better notation.</p> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=556392015-12-18T02:03:03Zyui-knk (Kaneko Yuichiro)
<ul><li><strong>Related to</strong> <i><a class="issue tracker-1 status-6 priority-4 priority-default closed" href="/issues/11618">Bug #11618</a>: Safe call syntax with aref or aset is </i> added</li></ul> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=596272016-07-15T18:49:43ZAnonymous
<ul></ul><p>It seems to me that a "safe subscript operator" should simply add a <code>&</code> between the receiver and the subscript operator (making <code>a[3]</code> safe would mean changing it to <code>a&[3]</code>), just like safe navigation adds a <code>&</code> between the receiver and the method invocation operator (<code>a.foo</code> => <code>a&.foo</code>).</p>
<p>Unfortunately, <code>&</code> is also a method name and is defined for several corelib classes (bitwise AND for <code>Fixnum</code>, set intersection for <code>Array</code>, boolean AND for <code>FalseClass</code>/<code>NilClass</code>/<code>TrueClass</code>). So if variable <code>a</code> above were an array, <code>a&[3]</code> would return the set intersection of <code>a</code> and <code>[3]</code>. It is true that <code>a&.[](3)</code> accomplishes the desired outcome, but this involves using the subscript operator as a method name -- which obscures semantic intent.</p>
<p>Is it possible to define a "safe subscript operator" with simple and unique syntax?</p> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=596562016-07-19T06:32:30Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Use <code>#dig</code> for referencing the value.<br>
For updating, show us use cases.</p>
<p>Matz.</p> Ruby master - Feature #11813: Extend safe navigation operator for [] and []= with syntax sugarhttps://bugs.ruby-lang.org/issues/11813?journal_id=654082017-06-16T13:27:42Zznz (Kazuhiro NISHIYAMA)
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/13645">Feature #13645</a>: Syntactic sugar for indexing when using the safe navigation operator</i> added</li></ul>