views:

123

answers:

5

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?

+2  A: 

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.

paxdiablo
+1  A: 

If you are asking about the technical ability to support both idioms, that's not a particularly interesting question. Just look at most modern languages and see how they do it. Usually, it's via some kind of catch-all dynamic type (Object in VB, dynamic in C#, void* in C, and so on).

At the metaphysical/ontological level, this question is much more interesting...

A theory in physics might suggest behaviour that defies intuition, leading one to ask, "How can that be?" For instance, the wave-particle duality goes beyond any commonsensical notion of how reality is or ought to be, and so it leaves us pondering the imponderable.

Programming languages, however, are not models of reality. They are inventions of the mind, designed to serve our purposes. It is thus meaningless to ponder how a programming language can be the way it is. It is that way because we wanted it to be that way; because it suits our purposes. No other reason is necessary or warranted.

So please understand that I am not being flippant or dismissive when I answer that a language can be both dynamic and static just because it can, and because this is useful. It is hopeless to try to probe any further.

Marcelo Cantos
A: 

C is a statically-typed language, but it has the flexibility to re-cast types to other types, and to use generic pointers (the void* type). The void* type means "a pointer to an unspecified type of data". Objective-C implements its dynamic types through the use of these void* types, though usually this is abstracted away by multiple levels of defines, typedefs, etc.

Ty
+1  A: 

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.

Barry Wark
A: 

Objective-C has a mixture of static and dynamic typing. The plain C objects are statically typed, but the Objective-C objects are dynamically typed. The Objective-C runtime does not care what type an object is as long as the messages you send to your objects are recognised by the object.

JeremyP