https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112020-10-19T00:15:06ZRuby Issue Tracking SystemRuby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880412020-10-19T00:15:06Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Project</strong> changed from <i>14</i> to <i>Ruby master</i></li></ul> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880432020-10-19T02:34:00Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>Hi, I'd like to add the background.</p>
<p>Currently, RBS provides <code>bool</code> type as an alias to <code>top</code> type (a union type of all types). The rationale is because any type is actually accepted in the context of condition expression of <code>if</code> and <code>while</code>. Some methods that accept a predicate block, such as <code>Enumerable#any?</code> and <code>#select</code>, also accept any type as the return value of the block.</p>
<p>However, (almost) all methods that end with <code>?</code> returns <code>true | false</code>. For example, of we write <code>bool</code> as the return type of <code>Array#empty?</code>, it means that the method may return any type, which is a bit less precise. <code>true | false</code> is a bit redundant, so <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/182">@marcandre (Marc-Andre Lafortune)</a> wants to write <code>Bool</code> for it, But in RBS, a capitalized type corresponds to Ruby's class or module. So, to make the story simple, he is proposing adding a <code>Bool</code> module in Ruby side instead of RBS.</p>
<hr>
<p>Personally, I'm unsure if it is good or not to change Ruby for this. If his proposal is accepted, the type of <code>Enumerable#any?</code> will be:</p>
<pre><code>def any? : { (Elem) -> bool } -> Bool
</code></pre>
<p>This looks cryptic to me. I think that the current statement (following) is good enough.</p>
<pre><code>def any? : { (Elem) -> bool } -> true | false
</code></pre>
<p>BTW, <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/370">@soutaro (Soutaro Matsumoto)</a> (the original author of RBS) is now thinking the redefinition of <code>bool</code> as an alias to <code>true | false</code>. Based on soutaro's idea, the type will be:</p>
<pre><code>def any? : { (Elem) -> top } -> bool
</code></pre>
<p>In terms of documentation, it loses the information that the return value of the block is considered as a condition, but I'm okay for it, too.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880442020-10-19T03:33:18Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p><code>Bool</code> and <code>bool</code> to have different semantics sounds problematic to me. Not against this proposed <code>Bool</code>, but we need to rename <code>bool</code> then.</p>
<p>BTW e.g. <code>class String; include Bool; end</code> shall be prohibited somehow. Maybe a parent class is better?</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880452020-10-19T12:20:43Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote in <a href="#note-3">#note-3</a>:</p>
<blockquote>
<p><code>Bool</code> and <code>bool</code> to have different semantics sounds problematic to me.</p>
</blockquote>
<p>As I stated, <code>String</code> and <code>string</code> have different semantics. Is that also problematic?</p>
<blockquote>
<p>Not against this proposed <code>Bool</code>, but we need to rename <code>bool</code> then.</p>
</blockquote>
<p>That would defeat the whole purpose.</p>
<blockquote>
<p>BTW e.g. <code>class String; include Bool; end</code> shall be prohibited somehow. Maybe a parent class is better?</p>
</blockquote>
<p>A parent class would work too, but one might still write <code>class Foo < Bool</code>...</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880462020-10-19T13:20:24ZEregon (Benoit Daloze)
<ul></ul><p>So <code>Bool</code> would be the "strict" variant, only <code>true | false</code>, and <code>bool</code> is anything convertible to a boolean, so any object, right?</p>
<p>I don't like the name <code>Bool</code>. If anything, I think it should be properly named <code>Boolean</code>.</p>
<p>I think adding <code>Boolean</code> on RBS' side (and not in Ruby) is not so bad.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=880512020-10-19T23:52:19Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Could you clarify the current proposal?</p>
<ul>
<li>bool / Bool / Boolean</li>
<li>RBS side only / Ruby module as well</li>
</ul>
<p>I am OK if RBS side only, no Ruby module.</p>
<p>Matz.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=881182020-10-22T10:41:56Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I tried to make a shorter summary of my thoughts here, so just three points - hopefully that makes it easier to read:</p>
<p>(1) Most of the discussion, I think, happened at <a href="https://github.com/ruby/rbs/issues/133" class="external">https://github.com/ruby/rbs/issues/133</a> - if I then understand the discussion correctly then it would mean that ruby would have to add "module Bool" or "module Boolean".</p>
<p>(2) One slight problem I see with that is that a use case originating (mostly) from RBS, even if it makes sense from the point of view of RBS, would potentially effect change in MRI ruby. I don't have a strong opposing opinion per se, but I think it would be better if the use case would originate from MRI directly, rather than the other way around. See also headius' comment in another issue about other implementations potentially affecting MRI via tests/specifications, without prior discussions. I am not involved in test/specs but I think I understood what headius meant here. This is one reason why I think it should be considered carefully whether change is really necessary in this regard. Keep also in mind that when a "module Bool / Boolean" exists in ruby, new users may ask how this should then be used, and it may be a bit complicated if the explanation is "because RBS uses it", even more so if these users may not need or use RBS (not everyone will use types; some may but others may not).</p>
<p>(3) I know way too little about the internals (admittedly I am not clever enough for high level C, and I am not even kidding here), but if the use case is primarily originating from RBS itself, could there not be another, indirect solution? For example, rather than requiring a change in MRI, could there not be some kind of meta-VM instead, that could be targeted? A bit like rubocop too, perhaps, for RBS? That way people could set it up for their own projects, adjust it as needed, and "simulate" as if a boolean "type" were to exist, without MRI needing to add a module, sort of where you just were to "simulate" that a boolean value exists. Again, I am not sure if this makes any sense what I write, but perhaps it would be better to wait some time, see how RBS shapes up, how the use cases may change, and then re-evaluate in say, two years or so. There are already quite a lot of changes if we look at the ruby-jit, ractor and so forth - it may be more clear how RBS may have to change (or effect change) in a little while.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=881212020-10-22T12:49:37ZDan0042 (Daniel DeLorme)
<ul></ul><p>mame (Yusuke Endoh) wrote in <a href="#note-2">#note-2</a>:</p>
<blockquote>
<p>BTW, <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/370">@soutaro (Soutaro Matsumoto)</a> (the original author of RBS) is now thinking the redefinition of <code>bool</code> as an alias to <code>true | false</code>.</p>
</blockquote>
<p>I think that's the better choice. Having <code>bool</code> as an alias to <code>top</code> is quite confusing. If we want to express that a method returns a truthy/falsy value, maybe <code>conditional</code> or <code>cond</code> would be a more meaningful alias for <code>top</code>.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=881382020-10-23T20:53:55Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>matz (Yukihiro Matsumoto) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>Could you clarify the current proposal?</p>
<ul>
<li>bool / Bool / Boolean</li>
<li>RBS side only / Ruby module as well</li>
</ul>
<p>I am OK if RBS side only, no Ruby module.</p>
<p>Matz.</p>
</blockquote>
<p>My preference is:</p>
<ul>
<li>
<code>Boolean</code>, second choice <code>Bool</code>
</li>
<li>Ruby side also, or if deemed not preferable then <code>RBS</code> only (but what if there's user module/class <code>Boolean</code>?)</li>
<li>if Ruby side, then base <code>Class</code>, second choice then <code>Module</code>.</li>
</ul>
<p>Ruby side has advantages beyound RBS, especially for communication with other typed systems / data interchange.</p>
<p>I hope that, in RBS, <code>String</code> is strict, <code>str</code> is relax. Same for <code>Boolean</code> (strict <code>true | false</code>) and <code>bool</code> is relax (any object).</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=881922020-10-26T07:55:15Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>RBS side is up to <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/370">@soutaro (Soutaro Matsumoto)</a> etc. I am still against Ruby side. <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/182">@marcandre (Marc-Andre Lafortune)</a> stated about communication with other typed systems / data interchange as a benefit, but we are not going to make Ruby (type-wise) compatible with statically typed languages. So its benefit is still unclear.</p>
<p>Matz.</p> Ruby master - Feature #17265: Add `Bool` modulehttps://bugs.ruby-lang.org/issues/17265?journal_id=882002020-10-26T09:44:08Zsoutaro (Soutaro Matsumoto)matsumoto@soutaro.com
<ul></ul><p>I'm planning to make the semantics of <code>bool</code> in RBS (in the next release). It will be an alias of <code>true | false</code> and we will add another type for conditionals, something like <code>type boolish = top</code>.</p>
<p>So, I feel there is no strong reason from RBS side to add new class/module to Ruby.</p>