> Le 27 mars 2019 à 18:26, Michael Theriot <[email protected]> a 
> écrit :
> 
> Would an error at runtime or compiletime occur here?
> 

The check cannot be reliably done at compile time in general; it must be a 
runtime check. (Tools may provide partial static analysis, but that’s an extra.)

The simplest reason is that statically typed code must run together with 
non-statically typed code.

And even ignoring legacy code, it is not desirable to have static types 
everywhere; for instance, `Array.prototype.sort()` must continue to work with 
both arrays of strings and arrays of numbers, and even with arrays of mixed 
types.

And even if everything everywhere was statically typed, you still cannot 
statically analyse situations like: `a = object[Math.random() < 0.5 ? 
"method_returning_number" : "method_returning_string"]()`.

> If it is a runtime error, is it optimal for the engine to keep track of typed 
> variables vs regular for the same value?

Today’s JS engines typically do already clever speculative optimisations based 
on expected types. For example: https://stackoverflow.com/a/46144917 
<https://stackoverflow.com/a/46144917>. So, I guess, it depends...

—Claude

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to