Programming Language Type Systems I


Static, dynamic, strongly-typed and/or weakly-typed; the terms we routinely use to describe typing in programming languages. However, these terms (especially the last two) have more to them than is generally assumed.

There is actually no widely-agreed upon definition for strong and weak typing – there are way too many contradictory views. Good news is that everyone agrees on dynamic and static typing, so that’s first.

Static typing

In a statically-typed language (e.g. Java, C#), type information is bound to the variable and checked at compile time. For example,

Java Example

String staticVariable = "static";
staticVariable = 1; //Error

The type of variable staticVariable will always be String, it can never be an Integer or Double. It might eventually be reassigned to a different string or even point to null but its type is NEVER going to change.

Dynamic typing

In a dynamically typed language (e.g. PHP, JavaScript), the type of a variable is bound to its value and type checking occurs at run time. This makes it possible to do things shown in the example below:

JavaScript Example

//Starts out as a string
var dynamicVariable = “string”;

dynamicVariable = 2; //Now an integer

dynamicVariable = [1,2,3]; //Now an array

dynamicVariable = {}; //Now an object

So which Typing class is better?

Please no language wars; both typing categories (static and dynamic) have their strengths and weaknesses.

With static languages, all operations are checked by the compiler, variables convey type information and compilation exposes bugs early. However, they are usually more verbose and it might be difficult to succinctly write versatile functions which accept multiple parameter types.

Dynamic languages are generally slower than static languages because methods and types have to be looked up and evaluated at run time. However, their low learning curves, flexibility and malleability make them perfect for quick prototyping.

Summary (tl;dr)

1. There is no universally-accepted definition for strong or weakly typed languages. (c’mon, computer scientists can surely do better huh?).

2. The static/dynamic delineation is completely independent of the strongly/weakly typed category, they are disjoint sets.

Insha Allaah my next post will be on Strong vs Weak typing; some explanation of the issues and why you should not use it too often in describing languages.

9 thoughts on “Programming Language Type Systems I

  1. “and it might be difficult to succinctly write versatile functions which accept multiple parameter types.”
    This is why static typed languages often have generics to allow for such functions.
    I personally like static typed languages because my comelier can help out filtering my stupidity easer and I have to write a lesser number of unit tests.
    Dynamic typed languages usually require more unit tests because there are more cases to test.

    Like

    1. Thanks Van Twisk.

      Generics sure do help a lot and I use them often too. But the build/compile time for statically-typed languages can be more than that for dynamic languages.

      I agree too that both approaches have their strengths and weaknesses.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.