Proposal: Optional Static Typing (Part 3)

Brandon Andrews warcraftthreeft at sbcglobal.net
Tue Jan 16 09:34:44 UTC 2018


Some follow-up as I think more about this.


> You can't freeze all builtins for obvious reasons.


I'm getting that the reason for not freezing them would be to define extensions? Could they not be defined and then frozen? I get that doesn't stop them from being dynamic still.


The ability to change the built ins like Object causes a number of issues as it makes all classes dynamic and your destructuring swap shows that well. It seems like as long as Object can be modified everything has to use run-time checking.


If Object could be made non-dynamic - froze it and made it const (or equivalent) - then one could write:


```js
const f = function(a:A)
{
        a.x = 0;
}
const A = new class
{
        x:uint8 = 5; // Object.defineProperty(A.prototype, 'x', { type: uint8, writable: true, value: 5 }); (I think, might have to think about this again, been a while).
}
f(new A()); // A is dynamic and the interpreter is forced to use a run-time check.

Object.freeze(A); // A is both const and frozen making it no longer dynamic? If the dynamicness was removed then the engine could search the code/AST and optimize f doing essentially a compile-time check at that point

f(new A()); // Compile-time verification for all instances of f where the argument is typed or the type can be inferred.
```


This usage of const works at global scope, but I feel like I'm missing something that would undermine this. Like without real namespace support this won't work well for some libraries. The syntax is also wordy and confusing. One could add const class A {} modifiers, but that's still confusing since codebases would be filled with it.


Also expecting users to freeze their classes to allow the interpreter and JIT to function sanely is asking a lot.


One problem that keeps bothering me is this delayed freezing doesn't help tooling. A class could be created, properties added in a complex operation, then the class frozen later. The tooling would be blind to all these changes.


I'm probably just barely touching the surface of issues. Anything I'm overlooking?


More information about the es-discuss mailing list