String finality (was: Re: ARGUMENTS.SHOULD.BE.ARRAY bug-fix)

Brendan Eich brendan at mozilla.org
Mon Mar 19 14:08:11 PDT 2007


On Mar 19, 2007, at 1:49 PM, Brendan Eich wrote:

> On Mar 19, 2007, at 1:01 PM, zwetan wrote:
>
>> but still out of curiosity,
>> in a case where someone would want to do the same kind of methods
>> delegation
>> but with non-generic methods (this value required to be of the
>> original type)
>> does ES4 offers some way of doing it ?
>
> Bound methods cannot be called on a |this| parameter of the wrong
> type -- they carry their |this| around once extracted (e.g. from a
> class instance).

I should have distinguished the case of an instance-bound method,  
where |this| carried along by the method closure, from a type- 
constrained |this| method, where you could extract and call on a  
different instance, but the type would still be constrained to be a  
subtype of the type from which you extracted the method.

We allow both cases to be expressed. To see both instance-bound and  
type-constrained |this| methods, consider this class:

class C {
   public var x;
   function C(x) : x = x { }

   // An instance-bound class method -- a "fixture"
   // (DontEnum/DontDelete/ReadOnly and type-constrained
   // by its function type).
   function m() { return x; }

   // An unbound prototype method with type-constrained |this|.
   prototype function p(this: {x:*}) { return this.x; }
};

let c = new C(42);
let m = c.m;
print(m());                   // prints 42

print(c.p());                 // prints 42 also

let o = {m: m, x:43};
print(o.m());                // prints 42 still

o.m = c.p;
print(o.m());                // prints 43

So class instance methods are instance-this-bound. Prototype methods  
(among other features) are unbound and by default their |this|  
parameter is not type-constrained. But you can write (this:T at the  
start of a formal parameter list or function type signature to  
constrain the type of |this|.

/be



More information about the Es4-discuss mailing list