Sep 27 meeting notes
Allen Wirfs-Brock
allen at wirfs-brock.com
Thu Sep 29 09:41:31 PDT 2011
On Sep 28, 2011, at 7:40 PM, Oliver Hunt wrote:
> class Foo {
> var bar; // default value -> undefined
> var wibble = 5; // initialised fields desugar to statements at the head of the constructor
> const wiffle; // read barrier on uninitialised
But note that such read barriers must be part of every property access including simple method invocations because the instance can escape from the constructor before it is initialized.
>
> function someMethod(..) {
> this.bar = foo; // could have a short hand for this.property
> }
>
> constructor (bar, wibble) {
> // extra typing due to the removal of horrible punning
> // but "public bar = bar" or whatever that syntax was is longer
> // than this.bar
theWiderWorld(this); //this escapes from constructor before wibble is initialized
> this.bar = bar;
> this.wibble = wibble;
> }
> }
function elsewhere(obj,key) {
var z = obj[key]; //needs read barrier, object might be an incomplete Foo instance and key might be 'wibble'
...
}
To accommodate this, each property would seem to need an additional [[Uninitialized]] "attribute" (it could be represented as a distinguished value) which would have to be checked as part of every property access.
You could avoid this simply my not having such "const" declarations and leaving it to the class author to use Object.defineProperty to set the [[Writable]] attribute of such properties to false.
Alternatively, you could just not worry that a const property could escape into the wild in a writable state and the value undefined.
Presumably the job of the constructor is to establish invariants of the constructed instances. Some of those invariants may involve complex relationships among property values and between the new object and other objects. There are many ways that an object can escape from a constructor before these invariants are fully established. Why is the "const" invariant any more important than other invariants that we aren't attempting to guarantee?
allen
More information about the es-discuss
mailing list