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 6BB9F1F4B4 for ; Sun, 27 Dec 2020 16:08:31 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id C6ED7120A68; Mon, 28 Dec 2020 01:07:42 +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 5F1F9120A66 for ; Mon, 28 Dec 2020 01:07:40 +0900 (JST) Received: by filterdrecv-p3iad2-74bd9fb996-rxl6b with SMTP id filterdrecv-p3iad2-74bd9fb996-rxl6b-20-5FE8B179-1F 2020-12-27 16:08:25.574641711 +0000 UTC m=+1443980.190100279 Received: from herokuapp.com (unknown) by ismtpd0019p1iad2.sendgrid.net (SG) with ESMTP id cCXajKNxSuCpcGMk1qvJcQ for ; Sun, 27 Dec 2020 16:08:25.557 +0000 (UTC) Date: Sun, 27 Dec 2020 16:08:25 +0000 (UTC) From: eregontp@gmail.com Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 77648 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?KippOI8ZHtTweq7XfQzW93937kJ4QNWwSBuHnaMEcr10UQHCfHc24DmrIrz06N?= =?us-ascii?Q?UnVqiFhI1yO3pTXiVeLDFLORF9KQWkog7DFsuzX?= =?us-ascii?Q?Cn9La7lFLPpVFq0uN2hduaYR=2FEk4zrtp=2FpbTnH1?= =?us-ascii?Q?2c4aSi1zGnRoJYE1bQTk=2FEnz2NpcmMtpjIzlkX8?= =?us-ascii?Q?k5+iLvlvOQd2L1wJ3wrJWbcmOA6hii9cD98r5G=2F?= =?us-ascii?Q?C=2Fsp+3O2gGHS86nh0=3D?= To: ruby-core@ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== X-ML-Name: ruby-core X-Mail-Count: 101746 Subject: [ruby-core:101746] [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). Status changed from Open to Rejected There is no "compile time" for Ruby, and there is no way to execute arbitrary Ruby code at any other time than runtime. Simply use constants if you want to ensure things are computed once, or rely on the JIT if it's simple enough: ```ruby MILLION = 10**6 ``` ---------------------------------------- Feature #17474: Interpreting constants at compile time https://bugs.ruby-lang.org/issues/17474#change-89568 * Author: jzakiya (Jabari Zakiya) * Status: Rejected * 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/