From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id 308E31F4B4 for ; Mon, 19 Oct 2020 02:34:12 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id 16525120931; Mon, 19 Oct 2020 11:33:29 +0900 (JST) Received: from xtrwkhkc.outbound-mail.sendgrid.net (xtrwkhkc.outbound-mail.sendgrid.net [167.89.16.28]) by neon.ruby-lang.org (Postfix) with ESMTPS id A527F1208FA for ; Mon, 19 Oct 2020 11:33:26 +0900 (JST) Received: by filterdrecv-p3mdw1-76f79d8b59-s45p8 with SMTP id filterdrecv-p3mdw1-76f79d8b59-s45p8-20-5F8CFB18-40 2020-10-19 02:34:00.945580574 +0000 UTC m=+528694.609101151 Received: from herokuapp.com (unknown) by ismtpd0141p1mdw1.sendgrid.net (SG) with ESMTP id sFa19KpgSJ-YPdItEgOL6g for ; Mon, 19 Oct 2020 02:34:00.863 +0000 (UTC) Date: Mon, 19 Oct 2020 02:34:00 +0000 (UTC) From: mame@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 76294 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17265 X-Redmine-Issue-Author: marcandre X-Redmine-Issue-Assignee: matz X-Redmine-Sender: mame X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: =?us-ascii?Q?EJh2gqwnyqXtd++xo=2FinyA1V0bXouTB4FkWnzNiKb4=2FjR7HnomzvxSIJGPCG79?= =?us-ascii?Q?tJN7gjE56vrCiCg=2FtItEcLwBBq79wuLUn79Rdpf?= =?us-ascii?Q?0oTF4ZgxMqQEUvuZqSDhJFAvAugB1nQOww0EH8R?= =?us-ascii?Q?+H7YnqMoJTAqhJ5Hj1azukv0Lrir8D7NypqIV6Q?= =?us-ascii?Q?b1th1N8guIK3zgNspgXImzTSjhzT2t5KWceBIrk?= =?us-ascii?Q?WmTpXosROUd633rmo=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 100424 Subject: [ruby-core:100424] [Ruby master Feature#17265] Add `Bool` module X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17265 has been updated by mame (Yusuke Endoh). Hi, I'd like to add the background. Currently, RBS provides `bool` type as an alias to `top` type (a union type= of all types). The rationale is because any type is actually accepted in t= he context of condition expression of `if` and `while`. Some methods that = accept a predicate block, such as `Enumerable#any?` and `#select`, also acc= ept any type as the return value of the block. However, (almost) all methods that end with `?` returns `true | false`. For= example, of we write `bool` as the return type of `Array#empty?`, it means= that the method may return any type, which is a bit less precise. `true | = false` is a bit redundant, so @marcandre wants to write `Bool` for it, But = in RBS, a capitalized type corresponds to Ruby's class or module. So, to ma= ke the story simple, he is proposing adding a `Bool` module in Ruby side in= stead of RBS. --- Personally, I'm unsure if it is good or not to change Ruby for this. If his= proposal is accepted, the type of `Enumerable#any?` will be: ``` def any? : { (Elem) -> bool } -> Bool ``` This looks cryptic to me. I think that the current statement (following) is= good enough. ``` def any? : { (Elem) -> bool } -> true | false ``` BTW, @soutaro (the original author of RBS) is now thinking the redefinition= of `bool` as an alias to `true | false`. Based on soutaro's idea, the type= will be: ``` def any? : { (Elem) -> top } -> bool ``` In terms of documentation, it loses the information that the return value o= f the block is considered as a condition, but I'm okay for it, too. ---------------------------------------- Feature #17265: Add `Bool` module https://bugs.ruby-lang.org/issues/17265#change-88043 * Author: marcandre (Marc-Andre Lafortune) * Status: Open * Priority: Normal * Assignee: matz (Yukihiro Matsumoto) ---------------------------------------- 1-line Summary: `rbs` would benefit from the existence of common ancestor `= Bool` for `TrueClass` and `FalseClass`. Detail: Matz: I am aware you rejected a similar request, but could we revisit this = in light of RBS? One use case was for an easy way to check for `true` or `false` values, ins= tead of simply for truthiness (e.g. for data transfer, strict argument chec= king, testing, etc.) I believe there's a new use case: `RBS` In `RBS`, the most used types like `String` and `Integer` have types for "s= tring-like" and "integer-like" objects: `string` and `integer` (all lowerca= se). For example the signature for `Integer#>>` is: ``` def >>: (int) -> Integer ``` It accepts an `Integer` *or an object responding to `to_int`* (summarized b= y `int`) and returns an `Integer` (and never another class of object respon= ding to `to_int` or not). There is a similar idea with boolean values, where a method may accept any = object and will use it's truthiness, while returning `true | false`. For ex= ample one of the interface for `Enumerable#all?` should look like: ``` def all?: () { (Elem) -> bool } -> true | false ``` The user supplied block can return any value, and its truthiness (anything = else than `nil` or `false`) will be used to determine the result of `all?`.= That result will be `true | false`, and no other value. If RBS is to be popular, there will be *many* signatures for such predicate= s (in builtin Ruby, stdlib, any gems, applications, etc.). I feel the best = option would be `Bool`, if this would be reflected in Ruby itself. Proposal: a new global module called `Bool`, without any method of constant= , included in `TrueClass` and `FalseClass`. Following reasons for rejection were given at the time: > many gems and libraries had already introduced Boolean class. I don't wan= t to break them. I looked and found the [`bool` gem](https://rubygems.org/gems/bool) that de= fines a `Bool` module. My proposal is compatible. In any case, this gem loo= ks abandoned, the author Aslak Helles=F8y doesn't have the code on github, = the gem has had 7000 downloads in the past 6 years and [has no public rever= se dependency](https://rubygems.org/gems/bool/reverse_dependencies). It als= o fails to install on my machine. I am not aware of incompatibilities. > `true` and `false` are the only representative of true-false values. In = Ruby. `nil` and `false` are falsy values, and everything else is a true val= ue. There's no meaning for having a superclass of `TrueClass` and `FalseCla= ss` as `Boolean`. The proposal is exactly to be able to easily write about this duality of `B= ool` as having only `true` and `false` as members, and every Ruby object as= being implicitly convertible as being truthy or falsy (`bool` in RBS). Discussion in RBS: * https://github.com/ruby/rbs/issues/133 Previous feature requests for `Boolean`: * https://bugs.ruby-lang.org/issues/14224 * https://bugs.ruby-lang.org/issues/12515 -- = https://bugs.ruby-lang.org/ Unsubscribe: