https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-06-26T20:47:45ZRuby Issue Tracking SystemRuby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=863492020-06-26T20:47:45Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-1 priority-4 priority-default" href="/issues/16989">Feature #16989</a>: Sets: need ♥️</i> added</li></ul> Ruby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=871722020-08-24T15:18:51ZDan0042 (Daniel DeLorme)
<ul></ul><p>+1<br>
I think this is more important than having a general Set syntax as discussed in <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: Add syntax to import Set into core (Open)" href="https://redmine.ruby-lang.org/issues/5478">#5478</a>. Being able to use <code>%ws[foo bar].include?(str)</code> is a double-plus of not creating a new object each time and having O(1) efficiency.</p> Ruby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=873882020-09-03T03:03:27ZDan0042 (Daniel DeLorme)
<ul></ul><p>I just thought of something...<br>
In the same way that <code>"str".freeze</code> is optimized to be deduplicated, <code>%w[a b].include?(obj)</code> could be optimized so it becomes equivalent to <code>obj == -"a" || obj == -"b"</code>, or something around those lines. This would have the advantage that all existing ruby code that uses this pattern would automatically become faster, without having to convert to a new literal syntax.</p> Ruby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=874022020-09-03T14:03:24ZEregon (Benoit Daloze)
<ul></ul><p>Dan0042 (Daniel DeLorme) wrote in <a href="#note-3">#note-3</a>:</p>
<blockquote>
<p>I just thought of something...<br>
In the same way that <code>"str".freeze</code> is optimized to be deduplicated, <code>%w[a b].include?(obj)</code> could be optimized so it becomes equivalent to <code>obj == -"a" || obj == -"b"</code>, or something around those lines.</p>
</blockquote>
<p>That already works on TruffleRuby (and for more than this specific case), it needs a JIT, inlining (also through builtins like #include?) and escape analysis.</p> Ruby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=876852020-09-25T04:26:51Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>We are going to introduce built-in set, but not in 3.0 (too little time to implement it before 3.0 release).<br>
After merging built-in set, we will seriously consider this proposal.</p>
<p>Remaining issues:</p>
<ul>
<li>Name? <code>%ws</code> would be the first two character specifier after <code>%</code>. Is it reasonable? Or should we seek another name?</li>
<li>Frozen? <code>%w</code> returns non frozen array of non frozen strings. How should <code>%ws</code> behave?</li>
</ul>
<p>Matz.</p> Ruby master - Feature #16994: Sets: shorthand for frozen sets of symbols / stringshttps://redmine.ruby-lang.org/issues/16994?journal_id=877322020-09-25T20:15:46Znormalperson (Eric Wong)normalperson@yhbt.net
<ul></ul><p><a href="mailto:matz@ruby.or.jp" class="email">matz@ruby.or.jp</a> wrote:</p>
<blockquote>
<p>Remaining issues:</p>
<ul>
<li>Name? <code>%ws</code> would be the first two character specifier after <code>%</code>. Is it reasonable? Or should we seek another name?</li>
<li>Frozen? <code>%w</code> returns non frozen array of non frozen strings. How should <code>%ws</code> behave?</li>
</ul>
</blockquote>
<p>How about suffix notation similar to Regexp modifiers?</p>
<pre><code>[ 'foo', 'bar' ]s
</code></pre>
<p>Or with ability to specify ordering:</p>
<pre><code>[ 'foo', 'bar' ]os # ordered set
[ 'foo', 'bar' ]us # unordered set
</code></pre>
<p>Fwiw, I sometimes wish I could use unordered hash to save space:</p>
<pre><code>{ 'foo' => 'bar' }u
</code></pre>
<p>And maybe 'f' modifier for frozen strings of values</p>
<blockquote>
<p><a href="https://bugs.ruby-lang.org/issues/16994#change-87685" class="external">https://bugs.ruby-lang.org/issues/16994#change-87685</a></p>
</blockquote>