5

Since Objective-C is a dynamic typing language, why do we still need types? Is the reason because it mixes with C code?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
lingguang1997
  • 451
  • 1
  • 4
  • 12
  • See http://stackoverflow.com/questions/5555855/dynamic-typing-objective-c-how-does-it-work – onnoweb Aug 28 '13 at 17:27
  • 6
    It's not just mixed with C code, it's a strict superset of C. – Wooble Aug 28 '13 at 17:27
  • 1
    You can, in theory, code everything as `id`, but you'd get yourself confused very quickly. (It is true that Objective-C is a curious blend of strong typing and duck typing. It's an odd beast.) – Hot Licks Aug 28 '13 at 17:31
  • 1
    You are essentially correct -- when you use static typing (as other than a hint to the Objective-C preprocessor to avoid warning messages) you are using C, not the Objective-C extensions. Objective-C itself does not *need* (or really even *use*) static types (though static types are helpful to humans to avoid coding bugs and general confusion). The one exception to this is for scalar parameters on an Objective-C call (though one could argue that that's C, not Objective-C, since the parms are passed through the preprocessor essentially unchanged). – Hot Licks Aug 31 '13 at 11:37

2 Answers2

18

Objective-C is not mixed with C, it's a proper superset of C with some dynamic features.

You can avoid the use of static typing on any Objective-C object variable and declare everything as id, but I don't see why would you want to do that.
You would lose all the help the compiler is giving you thanks to the type information (provided or inferred).

To wrap it up, you don't need types, you want types.

Community
  • 1
  • 1
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
  • 3
    Exactly; without strong typing of objects, what would be a compile time error turns into a runtime error, often at runtime in the hands of your [potential] customers.... Weakly typed languages *suck* for building complex applications. – bbum Aug 28 '13 at 17:56
  • @bbum - I wouldn't state it quite so strongly, but you have a point. – Hot Licks Aug 28 '13 at 17:59
  • I wouldn't exactly call Objective-C a "superset" of C. It's a SmallTalk-style language implemented via a sort of macro processor, running atop C. That you can still "reach" C is something of a coincidence (though one that the developers take advantage of to avoid the need to fully implement the "guest" language). – Hot Licks Aug 28 '13 at 18:02
  • 5
    The fact that you can "reach" C is exactly why it's a superset. It doesn't hide C, it includes it. From wiki: *it is possible to compile any C program with an Objective-C compiler, and to freely include C code within an Objective-C class*. Relevant: http://stackoverflow.com/questions/2963832/is-objective-c-2-0-a-proper-superset-of-c – Gabriele Petronella Aug 28 '13 at 18:04
  • @HotLicks Unlike C++, Objective-C **is** a superset of C. –  Aug 30 '13 at 22:21
  • In the same sense that printf is a superset of C. – Hot Licks Aug 31 '13 at 11:27
  • @HotLicks `printf` wasn't a language last time I checked – Gabriele Petronella Aug 31 '13 at 11:33
  • Some would argue the same for Objective-C ;) – Hot Licks Aug 31 '13 at 11:39
0

Objective-C is a proper superset of C as it can do everything that C can do, and more. Initially Objective-C was created as an extension of C which compiled on any C compiler.

Types can both assist you as a programmer, as well as aid in the description of the language semantics. They prevent errors and can increase the performance of your application since the runtime system does not have to work as hard.

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • Where can static type information increase performance in Objective-C? – Nikolai Ruhe Aug 28 '13 at 19:20
  • An example would be in a iterative construct. The runtime system would have to check on each iteration that the currently associated type of a variable can be used with boolean operators. – Pétur Ingi Egilsson Aug 28 '13 at 19:42
  • 2
    I don't really understand which scenario/language you describe but it's certainly not true for Objective-C. Its late binding means that on **each** message being sent the implementation of the method is being resolved from the selector again. – Nikolai Ruhe Aug 29 '13 at 09:31
  • Basically, when you're using static typing you're using C, not Objective-C. At most, static typing just clues the Objective-C preprocessor to produce the warning message that a method may not be available. – Hot Licks Aug 31 '13 at 11:30