Typing

From HaskellWiki
Revision as of 14:38, 27 November 2007 by Lemming (talk | contribs) (object orientation: dynamic+static typing)
Jump to navigation Jump to search

Programming languages can be distinguished by their type systems.

No vs. weak vs. strong typing

  • No typing: The language has no notion of types, or from a typed perspective: There is exactly one type in the language. Assembly language has only the type 'bit pattern', Rexx and Tk have only the type 'text', core MatLab has only the type 'complex-valued matrix'.
  • Weak typing: There are only few distinguished types and maybe type synonyms for several types. E.g. C uses integer numbers for booleans, integers, characters, bit sets and enumerations.
  • Strong typing: Fine grained set of types like in Ada, Wirthian languages (Pascal, Modula-2), Eiffel

Dynamic vs. static typing

This question concerns whether types are checked at run-time or at compile-time. Static typing has the advantage that errors can be catched before the program runs. This is especially useful if in a large library or program the type of a basic function is changed and this change affects many parts of the project. The type checker can spot many places where the program must be adapted. Static types also allow more efficient code, because if the type of a variable is 'ASCII character' the compiler knows that it must reserve a byte for it. The program does not need to allocate memory for it at run-time. If the variable is processed, its type needs not to be checked, actually the program even cannot check if the type is correct. The program just assumes that the place where the variable is stored always contains an ASCII character.

Dynamic typing is needed whenever the types are not known at compile time. Haskell provides the type Dynamic, which can safely be used but is hardly needed. Scripting languages usually rely entirely on dynamic typing.

  • Dynamically typed: Scripting languages (Perl, PHP, Python, Ruby), PROLOG
  • Statically typed: C, Ada, Wirthian languages, Eiffel, Haskell

Object oriented type systems try a balancing act between dynamic and static typing. Each reference has a static type, but at runtime you can assign more an object of a more specialised type to it. You need type information and type checks at run-time, but you have also static checks.