From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: poffice@blade.nagaokaut.ac.jp Delivered-To: poffice@blade.nagaokaut.ac.jp Received: from kankan.nagaokaut.ac.jp (kankan.nagaokaut.ac.jp [133.44.2.24]) by blade.nagaokaut.ac.jp (Postfix) with ESMTP id 995911AE0071 for ; Fri, 9 Sep 2016 07:55:02 +0900 (JST) Received: from voscc.nagaokaut.ac.jp (voscc.nagaokaut.ac.jp [133.44.1.100]) by kankan.nagaokaut.ac.jp (Postfix) with ESMTP id 22129B5D833 for ; Fri, 9 Sep 2016 08:27:34 +0900 (JST) Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by voscc.nagaokaut.ac.jp (Postfix) with ESMTP id 4BF7918CC80D for ; Fri, 9 Sep 2016 08:27:34 +0900 (JST) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id AD89E120477; Fri, 9 Sep 2016 08:27:32 +0900 (JST) X-Original-To: ruby-core@ruby-lang.org Delivered-To: ruby-core@ruby-lang.org Received: from o10.shared.sendgrid.net (o10.shared.sendgrid.net [173.193.132.135]) by neon.ruby-lang.org (Postfix) with ESMTPS id 2D696120459 for ; Fri, 9 Sep 2016 08:27:28 +0900 (JST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sendgrid.me; h=from:to:references:subject:mime-version:content-type:content-transfer-encoding:list-id; s=smtpapi; bh=DzVRhbzyWu/02PWyRA3xu6Oi1N4=; b=EizuCIql0oLIkWU0a1 GGlb8l/1F28DAxVdXZSCsznufMSA1DY25EQK/zfsSxz9Gb39sHZSFGx94/yBqtcE D1GE0LWZjTF8wkr1DRa4cM4EE+AGARI4r9YrNukkzVSCJmpAiZeDJ5/tdEjeAoY2 TXgej4R5vQBMmY8ozrX2Kvgts= Received: by filter0950p1mdw1.sendgrid.net with SMTP id filter0950p1mdw1.4190.57D1F3D729 2016-09-08 23:27:19.554627149 +0000 UTC Received: from herokuapp.com (ec2-54-163-214-37.compute-1.amazonaws.com [54.163.214.37]) by ismtpd0003p1iad1.sendgrid.net (SG) with ESMTP id XOzg0gMRQz2m0RpkLkiQKw Thu, 08 Sep 2016 23:27:19.535 +0000 (UTC) Date: Thu, 08 Sep 2016 23:27:18 +0000 From: shugo@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 52021 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 12086 X-Redmine-Issue-Author: shugo X-Redmine-Sender: shugo 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: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS5b499chpijTF5qf7QpOZt/bnTqC7gulTpMpM rrFb2J7f2g7o2Hq0/zAz7fvivrOvoAay7bNMySYD2te48XgRpmMTQQ3znxCOTOoNZjBGAY09sztnb+ vVwM/ZB8Ltz3S/GktTuM6NczTP2Wbu2A4TblIfW6Z1K/mVLd+x19AenjHA== X-ML-Name: ruby-core X-Mail-Count: 77223 Subject: [ruby-core:77223] [Ruby trunk Feature#12086] using: option for instance_eval etc. 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 #12086 has been updated by Shugo Maeda. Thomas Enebo wrote: > What is the scope of instance_eval here? Can I do: The answer is yes, in my original proposal. But It may be possible to prohibit these uses. If we add such a restriction, the following way suggested by Charles might be better: ``` require "radd_djur" calc = RaddDjur::Grammar.new(:expr) { using RaddDjur::DSL define :expr do # refined call to "define" [:int, "+", :int].bind { |x, *, y| # refined "bind" ret x + y # refined "ret" and "+" } / # refined "/" [:int, "-", :int].bind { |x, *, y| # etc ret x - y } ... ``` > Either of these essentially makes a lexically defined feature into a non-lexical one. It also means absolutely any code in the system may potentially be refined. Yes. ---------------------------------------- Feature #12086: using: option for instance_eval etc. https://bugs.ruby-lang.org/issues/12086#change-60450 * Author: Shugo Maeda * Status: Open * Priority: Normal * Assignee: ---------------------------------------- Currently refinements can be activated only in toplevel or class/module definitions. If they can be activated in block-level, it's useful to implement internal DSLs. How about to add a new option using: for Kernel#instance_eval and Moule#{class,module}_eval? ```ruby module FixnumDivExt refine Fixnum do def /(other) quo(other) end end end p 1 / 2 #=> 0 instance_eval(using: FixnumDivExt) do p 1 / 2 #=> (1/2) end p 1 / 2 #=> 0 ``` Proof-of-concept implementation is available at . In my previous proposal before Ruby 2.0, refinements used in a class or module are implicitly activated by instance_eval and class_eval, but now I think it's better to explicitly specify refinements to be activated. Considerations: * In the PoC implementation, refined methods are not cached inline, and thus it decreases the performance of refined method call. If there is a way to guarantee that blocks never be evaluated in different environments, refined methods can be cached inline. * {instance,class,module}_exec cannot be extended in the same way, because they take arbitrary arguments and there's no way to distinguish an option hash from the last argument hash. -- https://bugs.ruby-lang.org/