HACKER Q&A
📣 eimrine

Why isn't there a switch in the compiler from dynamic to static typing?


Inspired by the very active discussion on https://news.ycombinator.com/item?id=37764326


  👤 layer8 Accepted Answer ✓
Dynamic typing usually mean inserting hidden code for type checks at runtime, if the runtime environment doesn’t already perform such checks, which for statically typed languages it usually doesn’t. In other words, for the dynamic-typing option you either need a different runtime environment, or else the compiler needs to contain significant extra logic to generate code for the necessary runtime checks.

Effectively, this is not very different from having two different languages in the first place. With dynamic vs. static typing, you also generally want different standard libraries and language features, because those are two different modes of programming that can benefit from different affordances. For example, in a statically-typed language you might have function overloading. This wouldn’t work in a dynamically-typed language, or would have different semantics because it would dispatch on the runtime type instead of the static type. Meaning your proposed compiler switch would change which function is invoked.