Re: [LAU] OT: 2 minute intro to type systems (was C or C++?)

From: Erik de Castro Lopo <mle+la@email-addr-hidden-nerd.com>
Date: Thu Oct 14 2010 - 23:44:03 EEST

Erik de Castro Lopo wrote:

> Python and Ruby do have a strict type system, its just that
> the types are checked at run time. In these dynamically typed
> languages, *every* value is an object that caries around with
> it its type.

From at least one message in this thread it seems that there
is some confusion about type systems, in particular strict
vs strong typing.

Type systems can basically be mapped onto two axes;

                        strong
                           |
                           |
                           |
         dynamic ----------+---------- static
                           |
                           |
                           |
                         weak

Strong type systems prevent you from doing things that do not make
sense like adding an integer and a list. For instance, in Python,

>>> x = 1
>>> y = [ 1 , 2 ]
>>> x + y

gives an error like "unsupported operand type(s) for +: 'int' and 'list'".

Weak type systems allow things that don't make sense (possibly with
a user provided type cast). For instance in C:

       float test (GLIst *list)
       { int x = 10 ;
            return x + (int) list ;
       }

the above makes no sense whatsoever, but the compiler accepts the
programmers word that this is in fact what the programmer wants.

Purely static type systems are ones where all the types are checked at
compile time and the run time objects contain no type information
and no type checking is done at run time.

Purely dyanmic type systems are ones where every run time data
object carries around its type and all the type checking is done
at run time.

With the above axes set up you can have languages like C with
weak static types, Haskell with strong static types and Python
with strong dynamic types. I don't know of any language that
can really be said to have weak dynamic types.

Furthermore, most dynamically types languages have a small amount
of static type checking built in, but this is usually a by product
of syntax checking.

Similarly, in a statically typed langauge its possible to implement
a library with an object type where the type checking for operations
on those objects is carried out at run time.

Finally, many people assume that all strictly typed languages
require the programmer to explicitly define the types of everything.
Fortunately, this is not the case. There are a whole family of
languages that use Hindley-Milner type inference to infer the types
of all objects from the context. These languages include Ocaml and
Haskell.

HTH,
Erik

-- 
----------------------------------------------------------------------
Erik de Castro Lopo
http://www.mega-nerd.com/
_______________________________________________
Linux-audio-user mailing list
Linux-audio-user@email-addr-hidden
http://lists.linuxaudio.org/listinfo/linux-audio-user
Received on Fri, 15 Oct 2010 07:44:03 +1100

This archive was generated by hypermail 2.1.8 : Fri Oct 15 2010 - 00:15:06 EEST