return when desugaring to closures

David-Sarah Hopwood david.hopwood at industrial-designers.co.uk
Thu Aug 21 19:55:37 PDT 2008


Brendan Eich wrote:
> On Aug 21, 2008, at 6:45 PM, David-Sarah Hopwood wrote:
> 
>> In your example below, the opt_* variables are globally scoped in the
>> case where they are not affected by the 'with'. The global scoping is
>> not fully lexical (and I fully agree with your dismissal of the global
>> scope semantics as "hopeless"), but it isn't the 'with' construct that
>> is causing that.
> 
> Wrap my example in a function and then what kind of scoping do you have?

Lexical with conditional shadowing.

>> This is lexical scoping with conditional shadowing. If it were 
>> dynamic, then you'd expect:
>>
>>    function setOptionB(x) {
>>      print(x == opt_B);
>>    }
>>
>>    setOptions({opt_B: 42});
>>
>> to print true, but it actually prints false.
> 
> I don't see how. Dynamic scope means the caller's environment is visible 
> to the callee.

Yes, exactly. In your example, the caller's environment is not visible to
the callee for any of the calls.

> But an object initialiser passed as an actual argument 
> does not bind any of its properties in the caller's environment.

No, but the 'with' hypothetically would, if it provided dynamic scoping.
It's quite easy to imagine a 'with'-like construct that could work in
this way, in a language that was otherwise dynamically scoped, or if
the use of 'with' were to insert a scope in the lexical chain not
dependent on the with's lexical position. (Yes, that would be awful.)

> I've never heard of lexical scoping with conditional shadowing.

I just made up the term, but it's as good a description as any.

> Lexical scoping as I understand it means the binding environment can
> be judged statically (at compile time -- interesting contrast with
> dynamic typing,  but one does find the two paired often).

The distinction between lexical and dynamic scoping is (for the
definitions I prefer, anyway) decided by the algorithm that is used
to search for bindings:

  - for pure lexical scoping, look for a potential binding in the
    immediate lexically surrounding scope, then the next surrounding
    scope, and so on.
  - for pure dynamic scoping, look in the current activation record,
    then its caller's activation record, and so on; and finally
    the global scope if any.

In ECMAScript, the search for bindings is lexical. The reason why that
doesn't imply static analysability (besides other reasons like use of
'eval') is that when the search reaches a scope introduced by a 'with',
it may or may not find a match in that scope depending on the existence
of a property at run-time. But it's the sequence of scopes that are
searched that is the primary difference between the two categories of
scoping mechanism. (Hybrid mechanisms are also possible that use a
partly lexical and partly dynamic search, or that use different searches
for different variables -- Common Lisp does the latter.)

Of course, you can always adopt a definition of lexical scoping that
requires static analysability (and some sources do). In that case what
'with' does would be *neither* lexical nor dynamic scoping. But note that
by this definition lexical scoping would differ from dynamic scoping
along two logically distinct axes. It makes more sense to me to use
the term "static scoping" for scoping that is necessarily statically
analysable.

> Is there a programming language 
> of note with lexical scoping with conditional shadowing?

ECMAScript :-)

I don't know of anything that works like 'with' in any other programming
language. (Not directly as a language construct, I mean; you can obviously
simulate it in languages with eval, unhygienic macros, or computational
reflection.)

-- 
David-Sarah Hopwood


More information about the Es-discuss mailing list