I'm curious about how a compiled language can be dynamically typed. Typically, I think of compilers type checking code at compile time, which seems to lead to type errors being caught before the program runs. How does that work with dynamically typed languages? Does that mean some type checking happens at runtime instead?
6 Answers
When you create your own compiler, it doesn’t necessarily have to do type checking. For example, in C, if you declare all your variables as `void*`, you can cast them around freely—it becomes dynamic in that sense! Basically, the level of type checking can vary, and some languages choose not to enforce strict checks at compile time, allowing errors to surface as runtime issues instead.
Take a look at ANSI Common Lisp, which compiles down efficiently and is strongly but dynamically typed. Plus, it features a REPL for interactive coding, showcasing a blended approach to typing.
Overall, the distinction between compiled and dynamically typed languages isn’t black and white. Many languages implement a mix, balancing runtime checks and native compilation, depending on the goals of the language's design.
Using a `void*` pointer in C can mimic dynamic typing, as it can point to various data types. By adding a struct containing a pointer and a type_hint, you introduce a way to capture type information dynamically. It’s all about providing ways to find type information at runtime instead of relying strictly on static types at compile time.
Great point! For those interested, GLib and GTK demonstrate real-world implementations of dynamic typing discussed here.
Consider a language like Go. It’s compiled, yet it does some type-related checks at runtime, particularly when it comes to dereferencing variables. While it isn’t fully dynamic, it’s flexible enough. Similarly, C# has a `dynamic` keyword for scenarios that require it.
You're right! Just like C#, where `dynamic` allows for more flexibility, particularly with older COM objects.
There's really no absolute 'compiled language' definition. Python, for instance, can have 'compilers' that output optimized bytecode that still supports dynamic typing through techniques like runtime type checking—it's quite flexible depending on implementation.

Exactly! Most compilers include a type checking phase, but if they skip it, you’ll just run into type errors later, at runtime.