new instantiation design alternatives

Allen Wirfs-Brock allen at wirfs-brock.com
Fri Sep 12 08:26:27 PDT 2014


On Sep 11, 2014, at 11:39 PM, Jeff Morrison wrote:

> " If a constructor body contains an assignment of the form this = then automatic allocation is not performed and the constructor is expected to perform manual allocation."
> If I'm understanding this correctly, this means that snippet (A) would never have access to `this`, but snippet (B) would have an implicit `this` binding -- is that correct?
> 
> (A)
> ```
> class Foo extends Bar {
>   constructor() {
>     if (false) {
>       this = super();
>     }
>     this; // undefined
>   }
> }
> ```
> 
> (B)
> ```
> class Foo extends Bar {
>   constructor() {
>     // No call to `this =` present in the constructor 
>     this; // the automatically allocated object -- i.e. this !== undefined
>   }
> }
> ```
> 
> If this is the case, it occurs to me that it would raise issues for things like automated refactoring and/or dead code elimination (as a minifier might do).
> Normally a minifier (or even a human) would expect to be able to eliminate the entire conditional altogether if they were confident the condition never evaluated to true. But with this static pre-check acting as the indicator for whether automatic allocation/binding should happen, doing so would cause the constructor to act very unexpectedly differently in the two cases.
> 
> I wish I could suggest an alternative, but nothing comes to mind right now.

First a minor but technically important point:  `this` in (A) above is/isn't uninitialized rather than not having/having the value undefined. 

Good overall point , tools that eliminate dead code will need to be aware of the significant of `this =`.

Arguably, this is a point in favor of alternative two (no auto allocation).

Another possibility that we've considered is to tag non-auto-allocating constructors in their declaration header  For example:

constructor() new {...}

function foo() new {...}

But, overall, we were trying to minimize syntactic embellishments and bikesheding opportunities. 

Allen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140912/1bf6bfe7/attachment-0001.html>


More information about the es-discuss mailing list