Re: How to escape implicit 'with (this)' of a method body

2008-08-01 Thread Maciej Stachowiak

On Jul 31, 2008, at 5:24 AM, Dave Herman wrote:

 We should take this problem seriously. ...

 Dynamic scope is very bad.

 Specifically:

 - Classes are supposed to provide integrity, but dynamic scope makes  
 the
 internals of code brittle; any variable reference inside the
 implementation could be subverted by the seemingly innocuous insertion
 of a property.

 - Dynamic dispatch has a reasonably understandable cost model, but  
 only
 if it's confined to explicit property references. With dynamic scope,
 any variable reference could potentially be very expensive.

 - Generally, code within a `with' block is brittle and hard to
 understand, and as Tucker says, the implicit `this.' means that all  
 code
 inside class methods is within a `with' block... this means that all
 code inside class methods is brittle!

 - In the past, this has been enough for many programmers to deprecate
 all use of `with' -- we should certainly hope to avoid the same
 happening for classes.

I'm not sure of the benefits on the whole of implicit 'this' for class  
methods, but isn't it plausible to apply it only to static properties  
and not dynamically inserted ones, so all references continue to be  
bound at compile time and this sort of brittleness does not come up?

Regards,
Maciej

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-08-01 Thread Garrett Smith
On Fri, Aug 1, 2008 at 1:03 PM, Maciej Stachowiak [EMAIL PROTECTED] wrote:

 On Jul 31, 2008, at 5:24 AM, Dave Herman wrote:

 We should take this problem seriously. ...

 Dynamic scope is very bad.

 Specifically:

 - Classes are supposed to provide integrity, but dynamic scope makes
 the
 internals of code brittle; any variable reference inside the
 implementation could be subverted by the seemingly innocuous insertion
 of a property.

 - Dynamic dispatch has a reasonably understandable cost model, but
 only
 if it's confined to explicit property references. With dynamic scope,
 any variable reference could potentially be very expensive.

 - Generally, code within a `with' block is brittle and hard to
 understand, and as Tucker says, the implicit `this.' means that all
 code
 inside class methods is within a `with' block... this means that all
 code inside class methods is brittle!

 - In the past, this has been enough for many programmers to deprecate
 all use of `with' -- we should certainly hope to avoid the same
 happening for classes.

 I'm not sure of the benefits on the whole of implicit 'this' for class
 methods, but isn't it plausible to apply it only to static properties
 and not dynamically inserted ones,

What is dynamically inserted? I guess would mean properties added to
an instance of a non-sealed class.

 so all references continue to be
 bound at compile time and this sort of brittleness does not come up?


I think I remember discussion that 'this' in a static context was not valid.

If 'this' in a static context points to the class itself, it allows
for the possibility of the class having a static method, with a
private constructor and a public getInstance method with code
something like:

 class E { static function f(){ return new this; } }
 E.f()
[object E]

Works in the RI.

But I there was apparently a reason that that was not good, so that is a bug.

http://bugs.ecmascript.org/ticket/74

Garrett

 Regards,
 Maciej
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-08-01 Thread Brendan Eich
On Aug 1, 2008, at 2:43 PM, Garrett Smith wrote:

 What is dynamically inserted? I guess would mean properties added to
 an instance of a non-sealed class.

Right. Those should not be addressable by unqualified names in method  
scope -- you have to use this.

 so all references continue to be
 bound at compile time and this sort of brittleness does not come up?

 I think I remember discussion that 'this' in a static context was  
 not valid.

Maciej meant static in the compile-time or lexical sense, not static  
in the class singleton object property sense.

/be
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-31 Thread Peter Hall
 Here's my take at an example of brittleness:

 var bar = 42;

 class foo {
   function zot () { return bar; }
 }

 ...

 class subfoo extends foo {
   var bar = 'oops!';
 }

 (new foo).zot() = 42
 (new myfoo).zot() = ?


In AS3, the reference to bar in the zot function would be bound to
this.bar so, even in the subclass, there is no ambiguity and both
cases would output 42. I assume that ES4 would follow this behaviour.

The fragility is more likely to be in the opposite situation, where a
method in a class intends to access a global variable, but the
superclass has declared it too.


Peter
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-31 Thread P T Withington
On 2008-07-31, at 12:34EDT, Peter Hall wrote:

 Here's my take at an example of brittleness:

 var bar = 42;

 class foo {
  function zot () { return bar; }
 }

 ...

 class subfoo extends foo {
  var bar = 'oops!';
 }

 (new foo).zot() = 42
 (new myfoo).zot() = ?


 In AS3, the reference to bar in the zot function would be bound to
 this.bar

I don't follow.  There is no `this.bar` in the class where zot is  
defined.

 so, even in the subclass, there is no ambiguity and both
 cases would output 42. I assume that ES4 would follow this behaviour.

 The fragility is more likely to be in the opposite situation, where a
 method in a class intends to access a global variable, but the
 superclass has declared it too.

That was my original example, which would also exhibit fragility if  
the superclass is developed/evolves independently.  In either case,  
the fragility stems from the implicit (unreformed) `with this` in  
method bodies.
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-29 Thread P T Withington
On 2008-07-29, at 01:19EDT, Brendan Eich wrote:

 On Jul 28, 2008, at 10:05 PM, Jon Zeppieri wrote:

 The original code used without (this), not with, which I took to  
 mean avoid instance properties shadowing globals.

Indeed.  Perhaps I was being too clever in my pseudo-code.

 If you read the original as with, then there is no such problem.  
 But if you construct a problematic case using 'with' and dynamic  
 properties, then I concede that 'global' could be shadowed. This is  
 a reason to avoid 'with'. In the ES4 proposals last sent out, you  
 could always use __ES4__::global if you really wanted to avoid  
 conflicts -- unless someone perversely added '__ES4__' as a dynamic  
 instance property.

 There's no solution to this problem other than reserving at least  
 one name, and we can't do that compatibly. We could reserve __ES4__  
 in version-selected ES4 mode, but that seems unnecessary.

I guess this is considered a small penalty to pay in exchange for  
adding the magical instance scope to methods (which O-O programmers  
seem to expect these days).  Something we'd regret more if we had  
multi-methods, perhaps...
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


How to escape implicit 'with (this)' of a method body

2008-07-28 Thread Michael Haufe

/function foo () { return 'global'; }

class bar {
  function foo () { return 'local'; }

  function zot () {
// How can I call the global foo from here?
without (this) { foo(); }
  }
}/



You could use window[foo](); or whatever the global object is named in 
the environment
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-28 Thread Brendan Eich

On Jul 28, 2008, at 3:22 PM, Michael Haufe wrote:

function foo () { return 'global'; }

class bar {
   function foo () { return 'local'; }

   function zot () {
 // How can I call the global foo from here?
 without (this) { foo(); }
   }
}


It's the same as if you lambda-coded the above (here shown in JS1.8  
[Firefox 3], note the expression closures):


function bar() {
  function foo() 'local';
  function zot() global.foo();
}
function foo() 'global';

This example uses ES4's global synonym for the global object, but you  
could capture this in a global var at top level:


var global = this;
print(new bar().zot()); // print 'global'

in ES3 or JS1.8 to get the same effect.

You could use window[foo](); or whatever the global object is  
named in the environment


No need to quote and bracket, of course -- window.foo() is fine too.

/be

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-28 Thread Brendan Eich

On Jul 28, 2008, at 9:46 PM, Brendan Eich wrote:


On Jul 28, 2008, at 3:22 PM, Michael Haufe wrote:

function foo () { return 'global'; }

class bar {
   function foo () { return 'local'; }

   function zot () {
 // How can I call the global foo from here?
 without (this) { foo(); }
   }
}


It's the same as if you lambda-coded the above (here shown in JS1.8  
[Firefox 3], note the expression closures):


function bar() {
  function foo() 'local';
  function zot() global.foo();


+  return {foo: foo, zot: zot};


}
function foo() 'global';

This example uses ES4's global synonym for the global object, but  
you could capture this in a global var at top level:


var global = this;
print(new bar().zot()); // print 'global'

in ES3 or JS1.8 to get the same effect.


/be___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-28 Thread Jon Zeppieri
2008/7/29 Brendan Eich [EMAIL PROTECTED]:
 On Jul 28, 2008, at 3:22 PM, Michael Haufe wrote:

 function foo () { return 'global'; }

 class bar {
function foo () { return 'local'; }

function zot () {
  // How can I call the global foo from here?
  without (this) { foo(); }
}
 }

 It's the same as if you lambda-coded the above (here shown in JS1.8 [Firefox
 3], note the expression closures):
 function bar() {
   function foo() 'local';
   function zot() global.foo();
 }
 function foo() 'global';
 This example uses ES4's global synonym for the global object, but you could
 capture this in a global var at top level:
 var global = this;
 print(new bar().zot()); // print 'global'
 in ES3 or JS1.8 to get the same effect.

 You could use window[foo](); or whatever the global object is named in the
 environment

 No need to quote and bracket, of course -- window.foo() is fine too.
 /be

Isn't the 'with' statement in the original example significant?  In
the general case, assuming that you don't know what properties 'this'
has (as it may have dynamic properties in addition to the fixtures
determined by its class), you have no way of knowing whether 'global'
or 'window' refers to the global object or to some arbitrary property
of 'this.'

-Jon



 ___
 Es4-discuss mailing list
 Es4-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es4-discuss


___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: How to escape implicit 'with (this)' of a method body

2008-07-28 Thread Brendan Eich
On Jul 28, 2008, at 10:05 PM, Jon Zeppieri wrote:

 Isn't the 'with' statement in the original example significant?  In
 the general case, assuming that you don't know what properties 'this'
 has (as it may have dynamic properties in addition to the fixtures
 determined by its class), you have no way of knowing whether 'global'
 or 'window' refers to the global object or to some arbitrary property
 of 'this.'

The original code used without (this), not with, which I took to  
mean avoid instance properties shadowing globals. If you read the  
original as with, then there is no such problem. But if you  
construct a problematic case using 'with' and dynamic properties,  
then I concede that 'global' could be shadowed. This is a reason to  
avoid 'with'. In the ES4 proposals last sent out, you could always  
use __ES4__::global if you really wanted to avoid conflicts -- unless  
someone perversely added '__ES4__' as a dynamic instance property.

There's no solution to this problem other than reserving at least one  
name, and we can't do that compatibly. We could reserve __ES4__ in  
version-selected ES4 mode, but that seems unnecessary.

/be

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss