From: "headius (Charles Nutter)" Date: 2012-12-02T11:20:26+09:00 Subject: [ruby-core:50463] [ruby-trunk - Feature #4085] Refinements and nested methods Issue #4085 has been updated by headius (Charles Nutter). Anonymous wrote: > |This shouldn't work anyway. Why would we look at refinements for Integer if Fixnum has overridden those methods? This seems completely anti-OO to me. Are you saying refinements can route completely around overridden methods without touching the class that overrides them? > > If you think it's anti OO, you are too influenced by Java OO. Think > of CLOS's around methods. I want refinements to be method decorators. There is, of course, the concept of around methods in AOP. I'm familiar with the concept. I don't believe the definition of refinements as being like CLOS around methods had been mentioned up to this point. Is that the essential definition you'd like us to use for refinements going forward? Refinements are different from CLOS around methods in a number of crucial ways, though, as far as I understand them. * CLOS does not look at the call site to determine which around methods to call. Refinements must be active only for specific call sites. This makes implementing :around methods considerably easier, since we're always looking for :around, :before, :after methods on the target hierarchy rather than in some external floating structure. * As a necessary side effect of the above, :around methods apply to all calls equally. This avoids the readability challenges that refinements introduce. I'd love to see refinements go this way and not be derived from the calling scope. * :around methods are attached to the class hierarchy, not to an unrelated structure. This makes searching for them *much* easier. It also makes them more predictable...every call hits the same :around logic in the same order. The concept of :before, :after, and :around would be useful to introduce into Ruby. Refinements unfortunately conbine them with caller-scoped method lookup. Maybe what should go into 2.0 is just the :before, :after, :around capability without the caller-scoping? If we're going to look at refinements as :around methods, then your requirements for "super" would have to change. In CLOS, all :around methods fire all the time in the same order. Let's go with the original example of decorating Integer#/. You are right that the around logic is picked up even against Fixnum in CLOS :around methods: [1]> (defclass rinteger () ()) # [2]> (defclass rfixnum (rinteger) ()) # [3]> (defmethod div ((f rfixnum)) (print "div in fixnum")) #)> [4]> (defmethod div :around ((i rinteger)) (print "before div in integer") (call-next-method) (print "after div in integer") ) #)> [5]> (setq my-fix (make-instance 'rfixnum)) # [6]> (div my-fix) "before div in integer" "div in fixnum" "after div in integer" I've already pointed out the biggest difference: this applies to all call sites everywhere, not just some of them. So we're diverging from refinements a lot already. However, if we continue... Let's also define an around in rfixnum: [7]> (defmethod div :around ((i rfixnum)) (print "before div in fixnum") (call-next-method) (print "after div in fixnum") ) WARNING: The generic function # is being modified, but has already been called. #)> [8]> (div my-fix) "before div in fixnum" "before div in integer" "div in fixnum" "after div in integer" "after div in fixnum" The :around in rfixnum first, then the one in rinteger, then the actual div method. This is predictable behavior; all :around methods fire first, then :before methods, then the target method, then :after methods, and then control returns to the :around methods. This would require changes to the way methods are looked up in Ruby's hierarchy, but it would be easier than implementing refinements entirely outside the class hierarchy. It's worth pointing out that the order in which these :around methods are defined has no bearing on when they're dispatched; they obey the OO hierarchy: [14]> (defmethod div :around ((i rfixnum2)) (print "before div in fixnum") (call-next-method) (print "after div in fixnum") ) #)> [15]> (defmethod div :around ((i rinteger2)) (print "before div in integer") (call-next-method) (print "after div in integer") ) #)> [16]> (div (make-instance 'rfixnum2)) "before div in fixnum" "before div in integer" "div in fixnum" "after div in integer" "after div in fixnum" Refinements define their ordering completely outside the object hierarchy, which is one of my concerns about them. In fact, if you want something that's scoped but mimics :around methods, my original idea to have the methods in the class hierarchy would make the most sense. It would also match the definition-order-indepenent sequence in which the refinements are fired. The above could be modeled like this: module RefineInteger refine Integer do def /(other) puts "before / in RefineInteger" result = super puts "after / in RefineInteger" result end end end # At this point, the Integer class has been modified to recognize the above definition of "/" as an "around" method for the normal "/". module RefineFixnum refine Fixnum do def /(other) puts "before / in RefineFixnum" result = super puts "after / in RefineFixnum" result end end end # At this point, Fixnum has the above definition of "/" defined as an "around" method for the normal "/". using RefineFixnum # the Fixnum refinement is now active, but not the Integer refinement 1/1 # prints => before in RefineFixnum, after in RefineFixnum using RefineInteger # the Integer refinement is activated 1/1 # prints => before in RefineFixnum, before in RefineInteger, after in RefineInteger, after in RefineFixnum However this does not combine well with many files all defining their own refinements. CLOS does not handle such a case either...if you define two "around" methods at the same level in the hierarchy, the new one takes over the old one. [17]> (defmethod div :around ((i rfixnum2)) (print "new before div in fixnum") (call-next-method) (print "new after div in fixnum") ) WARNING: The generic function # is being modified, but has already been called. WARNING: Replacing method #)> in # #)> [18]> (div (make-instance 'rfixnum2)) "new before div in fixnum" "before div in integer" "div in fixnum" "after div in integer" "new after div in fixnum" This is obviously very different from refinements, where we may have multiple files define their own refinements. If all are in scope, they fire in the order in which they're defined...which will be unpredictable depending on the sequence of those loads. If some or all of them are not in scope, they will not fire at all. I am trying to understand what you envision refinements to be, but I'm having a very difficult time with it. ---------------------------------------- Feature #4085: Refinements and nested methods https://bugs.ruby-lang.org/issues/4085#change-34298 Author: shugo (Shugo Maeda) Status: Assigned Priority: Normal Assignee: matz (Yukihiro Matsumoto) Category: core Target version: 2.0.0 =begin As I said at RubyConf 2010, I'd like to propose a new features called "Refinements." Refinements are similar to Classboxes. However, Refinements doesn't support local rebinding as mentioned later. In this sense, Refinements might be more similar to selector namespaces, but I'm not sure because I have never seen any implementation of selector namespaces. In Refinements, a Ruby module is used as a namespace (or classbox) for class extensions. Such class extensions are called refinements. For example, the following module refines Fixnum. module MathN refine Fixnum do def /(other) quo(other) end end end Module#refine(klass) takes one argument, which is a class to be extended. Module#refine also takes a block, where additional or overriding methods of klass can be defined. In this example, MathN refines Fixnum so that 1 / 2 returns a rational number (1/2) instead of an integer 0. This refinement can be enabled by the method using. class Foo using MathN def foo p 1 / 2 end end f = Foo.new f.foo #=> (1/2) p 1 / 2 In this example, the refinement in MathN is enabled in the definition of Foo. The effective scope of the refinement is the innermost class, module, or method where using is called; however the refinement is not enabled before the call of using. If there is no such class, module, or method, then the effective scope is the file where using is called. Note that refinements are pseudo-lexically scoped. For example, foo.baz prints not "FooExt#bar" but "Foo#bar" in the following code: class Foo def bar puts "Foo#bar" end def baz bar end end module FooExt refine Foo do def bar puts "FooExt#bar" end end end module Quux using FooExt foo = Foo.new foo.bar # => FooExt#bar foo.baz # => Foo#bar end Refinements are also enabled in reopened definitions of classes using refinements and definitions of their subclasses, so they are *pseudo*-lexically scoped. class Foo using MathN end class Foo # MathN is enabled in a reopened definition. p 1 / 2 #=> (1/2) end class Bar < Foo # MathN is enabled in a subclass definition. p 1 / 2 #=> (1/2) end If a module or class is using refinements, they are enabled in module_eval, class_eval, and instance_eval if the receiver is the class or module, or an instance of the class. module A using MathN end class B using MathN end MathN.module_eval do p 1 / 2 #=> (1/2) end A.module_eval do p 1 / 2 #=> (1/2) end B.class_eval do p 1 / 2 #=> (1/2) end B.new.instance_eval do p 1 / 2 #=> (1/2) end Besides refinements, I'd like to propose new behavior of nested methods. Currently, the scope of a nested method is not closed in the outer method. def foo def bar puts "bar" end bar end foo #=> bar bar #=> bar In Ruby, there are no functions, but only methods. So there are no right places where nested methods are defined. However, if refinements are introduced, a refinement enabled only in the outer method would be the right place. For example, the above code is almost equivalent to the following code: def foo klass = self.class m = Module.new { refine klass do def bar puts "bar" end end } using m bar end foo #=> bar bar #=> NoMethodError The attached patch is based on SVN trunk r29837. =end -- http://bugs.ruby-lang.org/