torsdag, augusti 16, 2007

An introduction to categories of type systems

Since the current world is moving away from languages in the classical imperative paradigm, it's more and more important to understand the fundamental type differences between programming languages. I've seen over and over that this is still something people are confused by. This post won't give you all you need - for that I recommend Programming Language Pragmatics by Michael L. Scott, a very good book.

Right now, I just wanted to minimize the confusion that abounds surrounding two ways of categorizing programming languages. Namely strong versus weak typing and dynamic versus static typing.

The first you need to know is that these two typings are independent of each other, meaning that there are four different types of languages.

First, strong vs weak: A strongly typed language is a language where a value always have the same type, and you need to apply explicit conversions to turn a value into another type. Java is a strongly typed language. Conversely, C is a weakly typed language.

Secondly, dynamic vs static: A static language can usually be recognized by the presence of a compiler. This is not the full story, though - there are compilers for Lisp and Smalltalk, which are dynamic. Static typing basically means that the type of every variable is known at compile time. This is usually handled by either static type declarations or type inference. This is why Scala is actually statically typed, but looks like a dynamic language in many cases. C, C++, Java and most mainstream languages are statically typed. Visual Basic, JavaScript, Lisp, Ruby, Smalltalk and most "scripting" languages are dynamically typed.

See, that's not too hard, is it? So, when I say that Ruby is a strongly, dynamically typed language, you know what that means?

C is a actually an interesting beast to classify. It's the only weakly, statically typed language I can think of right now. Anyone has any more examples?

To find out more, read the book above, or look up "Type systems" on Wikipedia.

5 kommentarer:

Anonym sa...

Thanks, interesting info.
See also http://eli.thegreenplace.net/2006/11/25/a-taxonomy-of-typing-systems/

Jim Weirich sa...

C is a actually an interesting beast to classify. It's the only weakly, statically typed language I can think of right now.

FORTRAN?

PHT sa...

Thanks, interesting summary.

A question though : would you have an example of 'C' being weekly typed ? At first I though you were refering to the ability to 'void' cast everything, but then that would be 'explicit convertion' to me.

Sorry if that is obvious and I just can't remember it, I haven't been coding in C for quite so time ...
Regars

Sidu sa...

char c = 'a';
int i = c;
printf ("%d", i)

is what I think Ola meant about C being weakly typed.

BTW, could you please also explain what inferred types are? I came across the term when reading up on Scala.

Thanks.

Anonym sa...

@sidu,

Type inference is where the compiler is able to determine the type of something without an explicit annotation. Languages like Java make most types explicitly annotated, you _must_ write the type of a variable. Many ML-derived languages (Haskell, OCaml) require you to write type declarations in very few instances, they compiler is able to determine types from the expressions themselves.

For example, in Scala (a language unifying Object-Oriented and Functional programming on the JVM) you may omit the return type of a method (it will be inferred) unless that method is recursive.