|
|
|
|
|
by chriswarbo
4269 days ago
|
|
> The compiler gives an opinion "x has an inconsistent type". > You can still run the program if you like; then that opinion becomes an exception if an input case exercises the code, proving the compiler's opinion right. In this example, the compiler cannot give "x" the static type of "string" and also allow the program to handle errors when "x" is used in places where strings aren't allowed. If the static types don't match, the result is undefined; that's what static typing means. If the compiler causes an exception to be thrown, then either: - That's just an implementation-specific quirk, which just-so-happens to be the way this compiler behaves when it hits this undefined case; it's not part of any spec/documentation and useful only to hackers trying to exploit the system. - We accept that the compiler didn't assign "x" the static type of "string" after all; it actually assigned it "string + exception + ..." (which may be the "any" type of the dynamic language, or some sub-set of it) |
|
> What about compilers for dynamic languages ...
A compiler for a dynamic language that checks types does not bring about static typing. The language in fact remains dynamic.
What the checking means is that the compiler can give an opinion based on a static view of the program, and we can run that program regardless.
The compiler can say: yes, all expressions in the program can be assigned a type; no, some expressions have a conflicting type, or couldn't be assigned a type.
Static typing indeed means that we use the result of the static analysis to remove all traces of type from the program, and only run it when its type information is complete and free of conflicts.
The dynamic language optimizer can in fact take advantage of its findings to eliminate run-time type checks where it is safe to do so.