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=-2.6 required=3.0 tests=AWL,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY shortcircuit=no autolearn=no 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 0C1D21F4B4 for ; Mon, 4 Jan 2021 12:37:40 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id CD52B120C03; Mon, 4 Jan 2021 21:36:49 +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 B59A0120C02 for ; Mon, 4 Jan 2021 21:36:44 +0900 (JST) Received: by filterdrecv-p3las1-685fdc5bbc-t824x with SMTP id filterdrecv-p3las1-685fdc5bbc-t824x-19-5FF30C06-9 2021-01-04 12:37:26.288789226 +0000 UTC m=+2122509.437071399 Received: from herokuapp.com (unknown) by geopod-ismtpd-4-8 (SG) with ESMTP id KxH7P0XQToGP3ipK_vc-yw for ; Mon, 04 Jan 2021 12:37:25.959 +0000 (UTC) Date: Mon, 04 Jan 2021 12:37:26 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 77817 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 17474 X-Redmine-Issue-Author: jzakiya X-Redmine-Sender: Eregon 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr1y1lNnV349lqMs+5Elfb?= =?us-ascii?Q?zl7bBGkBzZPSYXVGMyoxfZ9ewB6LIdl2M5jbYBg?= =?us-ascii?Q?kwWcEmUgFne2Mtq6jM+gqES1zieIS1IMwGJxkEw?= =?us-ascii?Q?AeYb21ex3PZ26Q084EYamlxQmXklWxcMF5u50Wj?= =?us-ascii?Q?2l=2Fsy=2FJLXfRJ6xOL=2F8ByVlSjtStDkjd5n4tr8ZL?= =?us-ascii?Q?mi5hdpPd=2Fn9cb4ux4=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 101910 Subject: [ruby-core:101910] [Ruby master Feature#17474] Interpreting constants at compile time 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="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" Issue #17474 has been updated by Eregon (Benoit Daloze). jzakiya (Jabari Zakiya) wrote in #note-16: > **_would not_** work because it doesn't evaluate at parse-time to a constant value. It's not possible to evaluate any Ruby code except literals at parse time, as I already said in https://bugs.ruby-lang.org/issues/17474#note-8. C++ has a concept of compile-time expressions/constants (constexpr), but it doesn't make any sense for Ruby, since most expressions in Ruby need the runtime to be evaluated. I think this feature would just confuse people. Constants are a thousand times clearer, and simpler. ---------------------------------------- Feature #17474: Interpreting constants at compile time https://bugs.ruby-lang.org/issues/17474#change-89755 * Author: jzakiya (Jabari Zakiya) * Status: Open * Priority: Normal ---------------------------------------- Ruby has borrowed concepts/idioms from allot of languages. I am proposing borrowing a feature from Forth to provide for compile time interpretation of Constants. This should make executed code faster|efficient, while maintaining source code brevity|clarity. Below is actual code used in a large rubygem I have. To develop this method, I had to do allot of test runs to determine the range values. Once found, these values don't change, but I just kept the computed forms of the values, in case I want to upgrade them. In Forth I can interpret those expressions that result in constants, which will be compiled as single values for run time. See wikeipedia article on Forth below starting at **Mixing states of compiling and interpreting**. https://en.wikipedia.org/wiki/Forth_(programming_language) Forth was designed for, and is still used most frequently, in hardware controllers, and with microprocessors. IMHO this feature would also make MRuby more code efficient and faster for this domain too, and IOT devices. Below is an example of real code that would benefit from this. While this example would result in numerical constant, string constants could also be interpreted. ``` def select_pg(endnum, startnum) start_num = end_num end_num = endnum; start_num = startnum range = end_num - start_num pg = 5 if start_num <= Integer.sqrt(end_num) # for one array of primes upto N pg = 7 if end_num > 50 * 10**4 pg = 11 if end_num > 305 * 10**5 else # for split array cases pg = 7 if ((10**6 ... 10**7).include?(range) && start_num < 10**8) || ((10**7 ... 10**8).include?(range) && start_num < 46 * 10**8) || ((10**8 ... 10**9).include?(range) && start_num < 16 * 10**10) || (range >= 10**9 && start_num < 26 * 10**12) pg = 11 if ((10**8 ... 10**9).include?(range) && start_num < 55 * 10**7) || (range >= 10**9 && start_num < 45 * 10**9) end primes = [2, 3, 5, 7, 11, 13].select { |p| p <= pg } {primes, primes.reduce(:*)} # [excluded primes, modpg] for PG end ``` Allowing for compile time interpretation, the code could be rewritten as below. ``` def select_pg(endnum, startnum) start_num = end_num end_num = endnum; start_num = startnum range = end_num - start_num pg = 5 if start_num <= Integer.sqrt(end_num) # for one array of primes upto N pg = 7 if end_num > [50 * 10**4] pg = 11 if end_num > [305 * 10**5] else # for split array cases pg = 7 if (([10**6] ... [10**7]).include?(range) && start_num < [10**8]) || (([10**7] ... [10**8]).include?(range) && start_num < [46 * 10**8]) || (([10**8] ... [10**9]).include?(range) && start_num < [16 * 10**10])|| (range >= [10**9] && start_num < [26 * 10**12]) pg = 11 if (([10**8] ... [10**9]).include?(range) && start_num < [55 * 10**7]) || (range >= [10**9] && start_num < [45 * 10**9]) end primes = [2, 3, 5, 7, 11, 13].select { |p| p <= pg } {primes, primes.reduce(:*)} # [excluded primes, modpg] for PG end ``` This maintains the original form, so if I need/want to change the range limits again I can just change the calculation inline, without having to remember where those values came from. As 3.0 has introduced many new features and idioms, this could be introduced with no breaking change too. Old code would work as before, while new code could take advantage of this feature. Thanks is advance of giving this proposal serious consideration. -- https://bugs.ruby-lang.org/