Statically-typed languages and dynamically-typed languages in principle seem like opposite concepts. However, how can a language like Objective-C for example be both of these things at once? It seems to me that Objective-C is more static than dynamic. Can somebody explain how this is possible?
A programming language is statically typed if the type of a variable is known at compile time. A language is dynamically typed if the type of a variable is checked during run-time.
A dynamic language (Lisp, Perl, Python, Ruby) is designed to optimize programmer efficiency, so you can implement functionality with less code. A static language (C, C++, etc) is designed to optimize hardware efficiency, so that the code you write executes as quickly as possible.
Dynamically-typed languages are those (like JavaScript) where the interpreter assigns variables a type at runtime based on the variable's value at the time.
Statically typed is a programming language characteristic in which variable types are explicitly declared and thus are determined at compile time. This lets the compiler decide whether a given variable can perform the actions requested from it or not. Static typing associates types with variables, not with values.
I believe you are confusing static typing and dynamic method resolution. Objective-C is definitely strongly, statically typed. Like C, all variables must be declared and typed (there isn't even type inference as in other modern, statically typed languages). The compiler generates code based on the type of variables and this type cannot be changed at runtime.
However, Objective-C method calls use a message passing paradigm, where the message name and target are encoded at compile time, but the address of the code to execute is looked up at runtime by the Objective-C runtime libraries.
Objective-C is really (conceptually) just a layer on the C language itself and, as such, can have both static and dynamic types. Static if you're using the base-C stuff, dynamic if you're using the Objective-C extensions.
But C also sort-of provides this feature. If you just think about the void * type in C, you'll see that it can point to any type, hence giving you a (very rough) dynamically-typed language.
For example:
int i;
float f;
double d;
void *p = &i;
p = &f;
p = &d;
At all those assignments to p
above, it's made to point to a different type. If you do your code cleverly enough, you can even emulate RTTI and polymorphism in C.
I would consider a language primarily statically or dynamically typed, based on what it was most used for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With