|
|
|
|
|
by kazinator
4273 days ago
|
|
The situation isn't one of static typing, but of static type checking. Check (parent (parent (parent yourpost))) where it says > 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. |
|
> 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.
What is a "conflicting type"? In the dynamic language, there is only one static type (any = int + string + float + (any -> any) + array(any) + ...), so there can't be any conflicts.
It's fine to have a compiler try to specialise the types of variables beyond that, eg. narrowing-down the type of "x" in the example to "string + float", based on how it's used (or just leave it as "any").
A "conflict" would imply that a variable is used in ways that don't match the inferred type; but the inference is based on how it's used.