What is open recursion?

前端 未结 4 499
青春惊慌失措
青春惊慌失措 2021-02-01 16:34

What is open recursion? Is it specific to OOP?

(I came across this term in this tweet by Daniel Spiewak.)

相关标签:
4条回答
  • 2021-02-01 17:09

    just copying http://www.comlab.ox.ac.uk/people/ralf.hinze/talks/Open.pdf: "Open recursion Another handy feature offered by most languages with objects and classes is the ability for one method body to invoke another method of the same object via a special variable called self or, in some langauges, this. The special behavior of self is that it is late-bound, allowing a method defined in one class to invoke another method that is defined later, in some subclass of the first. "

    0 讨论(0)
  • 2021-02-01 17:18

    The name "open recursion" is a bit misleading at first, because it has nothing to do with the recursion that normally is used (a function calling itself); and to that extent, there is no closed recursion. It basically means, that a thing is referring to itself. I can only guess, but I do think that the term "open" comes from open as in "open for extension". In that sense an object is open to extension, but still referring to itself.

    Perhaps a small example can shed some light on the concept.

    Imaging you write a Python class like this one:

    class SuperClass:
       def method1(self):
           self.method2()
       def method2(self):
           print(self.__class__.__name__)
    

    If you ran this by

    s = SuperClass()
    s.method1()
    

    It will print "SuperClass".

    Now we create a subclass from SuperClass and override method2:

    class SubClass(SuperClass):
    def method2(self):
        print(self.__class__.__name__)
    

    and run it:

    sub = SubClass()
    sub.method1()
    

    Now "SubClass" will be printed.

    Still, we only call method1() as before. Inside method1() the method2() is called, but both are bound to the same reference (self in Python, this in Java). During sub-classing SuperClass method2() is changed, which means that an object of SubClass refers to a different version of this method.

    That is open recursion.

    In most cases, you override methods and call the overridden methods directly. This scheme here is using an indirection over self-reference.

    P.S.: I don't think this has been invented but discovered and then explained.

    0 讨论(0)
  • 2021-02-01 17:27

    This paper analyzes the possibility of adding OO to ML, with regards to expressivity and complexity. It has the following excerpt on objects, which seems to make this term relatively clear –

    3.3. Objects

    The simplest form of object is just a record of functions that share a common closure environment that carries the object state (we can call these simple objects). The function members of the record may or may not be defined as mutually recursive. However, if one wants to support inheritance with overriding, the structure of objects becomes more complicated. To enable open recursion, the call-graph of the method functions cannot be hard-wired, but needs to be implemented indirectly, via object self-reference. Object self-reference can be achieved either by construction, making each object a recursive, self-referential value (the fixed-point model), or dynamically, by passing the object as an extra argument on each method call (the self-application or self-passing model).5 In either case, we will call these self-referential objects.

    0 讨论(0)
  • 2021-02-01 17:35

    Open recursion allows to call another methods of object from within, through special variable like this or self.

    0 讨论(0)
提交回复
热议问题