2
$\begingroup$

I was not a big fan of Object-Oriented Languages (OOL), but recently started to learn a bit more about their pros and cons in a general setting instead of diving into one such language. I have a few questions here:

  1. Do methods in an OOL contain free variables? If they do, are they statically-scoped?

  2. Do method names live in a different name space than variables?

For both questions, I have kinda guess answers but am not quite sure. However, I would like to know more about the reasons behind and consequences of OOLs' specific answers to these questions.

$\endgroup$
  • 3
    $\begingroup$ probably better for cs.se $\endgroup$ – vzn Apr 2 '13 at 15:07
3
$\begingroup$

The answers to these questions vary with the specific OO language. Here are some fairly generic answers.

  1. Can methods contain free variables?

    In general yes.

  2. Are free variables statically-scoped?

    It's complicated. As a general rule (with many exceptions), most variables are lexically-scoped, except that the self reference (e.g., this in Java) is an implicit, dynamically-scoped variable. Many OO languages further rewrite function calls m() to a method invocation this.m() if m is known to be a method of the type of this.

  3. Do method names live in a different name space than variables?

    Yes. Method names are not variables: they may not be freely $\alpha$-renamed, since the actual text of the name constitutes part of the interface of an object. They are much more closely related to record labels or module component names. However, method names can interact with binding structure since many OO languages treat method invocations a bit specially (e.g., my answer to #2).

$\endgroup$
  • $\begingroup$ Thanks for the quick answer, Neel. Regarding Point 1, it seems to me that free variables in an OOL are mostly restricted to instance variables and class variables. So disregarding self or this, as long as the instance relation and the inheritance chain are maintained, there is no danger of getting into dynamic scope, right? Regarding Point 3, I do not quite get what you mean by "interacting with binding structure", could you clarify a bit? Thanks. $\endgroup$ – day Apr 2 '13 at 12:04
  • 2
    $\begingroup$ Almost all OO languages have local variables, and many also include first-class procedures (or objects) -- these are scoped lexically. If you disregard this, you don't really have an OO language, though! (Point 3 is nothing more than the fact that some OO languages let you shadow method names within a procedure by binding a local variable of the same name. Nothing hard is going on, though the details can get complicated/tedious.) $\endgroup$ – Neel Krishnaswami Apr 2 '13 at 12:45
  • $\begingroup$ Right, these local variables (instance variables, class variables are kinda global variables though) are lexically scoped because they are contained in the object, and by following this, they can be reached. Thanks for the further explanation. $\endgroup$ – day Apr 2 '13 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.