Why is it called "open (or closed) recursion?
Asked Answered
S

1

22

I found some explanations of open/closed recursion, but I do not understand why the definition contains the word "recursion", or how it compares with dynamic/static dispatching. Among the explanations I found, there are:

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 languages, 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. [Ralf Hinze]

... or in Wikipedia :

The dispatch semantics of this, namely that method calls on this are dynamically dispatched, is known as open recursion, and means that these methods can be overridden by derived classes or objects. By contrast, direct named recursion or anonymous recursion of a function uses closed recursion, with early binding.

I also read the StackOverflow question: What is open recursion?

But I do not understand why the word "recursion" is used for the definition. Of course, it can lead to interesting (or dangerous) side-effect if one uses "open recursion" by doing... a method recursion call. But the definitions do not take method/function recursive call directly into account (appart the "closed recursion" in the Wikipedia definition, but it sounds strange since "open recursion" does not refer to recursive call).

Do you know why there is the word "recursion" in the definition? Is it because it is based on another computer science definition that I am not aware of? Should simply saying "dynamic dispatch" not be enough?

Sitsang answered 23/7, 2013 at 7:12 Comment(1)
This is interesting bc I've never thought about "this/self" as being recursive, but of course, they areSlicer
F
32

I tried to start writing an answer here and then ended up writing an entire blog post about it. The TL;DR is:

So, if you compare a real object-oriented language to a simpler language with just structures and functions, the differences are:

  • All of the methods can see and call each other. The order they are defined doesn’t matter since their definitions are “simultaneous” or mutually recursive.
  • The base methods have access to the derived receiver object (i.e. this or self in other languages) so they don’t close over just each other. They are open to overridden methods.

Thus: open recursion.

Faithless answered 26/8, 2013 at 18:27 Comment(4)
Thank you! Your blog post is very clear and really answers my questions. So it all comes from functionnal programming vocabulary. Also thanks for your references.Sitsang
The mother of programming languages is lambda calculus, so everything relates to functions. But IMHO, the phrase "... all comes from functional programming vocabulary" is overstated; it is hard to reason why "open recursion" should exists (your question is a concrete example) without thinking about models of objects.Cauvery
@TaThanhDinh Actually it is not. All you need to distinguish "open" vs "close" is in name resolution, i.e. the process to determine the denotation of a name from the source code. Objects (or values) only come after this as the result. Unless you want do it in the metalanguage (often with some explicit forms of eval calls handling the expression "object" as its input), there are no objects required to exist at all. Notice how an entity can "see" others or not is a meta property under assumption of implicit evaluation rules.Calderon
@TaThanhDinh Moreover, this is why the object and functional paradigms complement each other so well, a la Scala, for example.Slicer

© 2022 - 2024 — McMap. All rights reserved.