[Grace-core] nesting - what should "self" be when invoking lexically scoped methods

Lex Spoon lex at lexspoon.org
Wed Aug 8 20:26:46 PDT 2012


Score one for the anti-shadowing rule! That's great that it prevents
this particular confusing code sample.


> Then the question is: when an implicit call resolves to a lexically scoped method,
> where does the *receiver* resolve to?
> To the outer object, or the inner one?
>
> I always assumed outer, but looking at the Newspeak spec, I think it implies
> such a call does not change the receiver.

Static typing -- and static understanding of code for humans --
requires that the dynamic behavior of "self" has some relation to the
static context the expression appears in. The simplest way to make
this so is if receivers stick to methods. That is, when a method is
invoked, the receiver becomes whatever object the method was attached
to.

Regarding other languages, Scala and Java have sticky receivers as I
described. I don't know Newspeak well, but i believe it does the same:

"When a user defined method f is invoked on an object o in response to a
message μ, an activation a (3.6) derived from f in response to μ is
instantiated.
The current instance of the activation is set to o."

"For closure activations, the current instance is the current instance
of the closure p."


JavaScript is different. It sets the receiver depending on how you
invoke the function (it doesn't have methods as a separate concept):

    a.foo()   // The receiver will be a
    var f = a.foo; f();    // The receiver will be the global "window" object

I believe the JavaScript semantics is frustrating to programmers. I
know it is for me! The "this" keyword taunts you to use it, but
whenever you write it down, you aren't completely sure what it is
going to do. How it will behave depends on how your function is
invoked in the future.

Lex


More information about the Grace-core mailing list