What does weak static typing/strong dynamic typing mean

programming-languagestype-systems

For most of my career, I've been working with strong static typed languaged such as Java. For this reason, probably I've mixed up these two typing dimension (strongness and staticness). I came upon this link, which says that they are actually orthogonal dimension. I read it, but still can't understand this distinction. 'weak static' sounds oxymoron to me, because static sounds like they will check stuff at compile time, while weak is the opposite.

The page uses Pascal and C as examples of weak static, and CommonLisp and Python as examples of strong dynamic, but unfortunately I don't know enough of these language to help.

What does weak static typing/strong dynamic typing mean? How can a language like C and Python be like Java in one aspect, but like Perl in others?

Best Answer

The terms strongly typed and weakly typed dont have a agreed-upon definition. Therefore, unless you define what you mean by "strongly typed" and "weakly typed", it is impossible to answer your question.

It makes a "great" argument in a flamewar, because whenever someone is proven wrong, they can just redefine the terms to mean whatever they want them to mean. Other than that, the terms serve no real purpose.

It is best to just not use the terms, or, if you use them, rigorously define them first. If you see someone else use them, ask them to define the terms.

This is what Benjamin C. Pierce, author of Types and Programming Languages and Advanced Types and Programming Languages has to say:

I spent a few weeks... trying to sort out the terminology of "strongly typed," "statically typed," "safe," etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless.

Luca Cardelli, in his Typeful Programming article, defines it as the absence of unchecked run-time type errors. Tony Hoare calls that exact same property "security". Other papers call it "type safety" or simply "safety".

Mark-Jason Dominus wrote a classic rant about this a couple of years ago on the comp.lang.perl.moderated newsgroup, in a discussion about whether or not Perl was strongly typed. In this rant he states that within just a few hours of research, he was able to find 8 different, sometimes contradictory definitions, mostly from respected sources like college textbooks or peer-reviewed papers. In particular, those texts contained examples that were meant to help the students distinguish between strongly and weakly typed languages, and according to those examples, C is strongly typed, C is weakly typed, C++ is strongly typed, C++ is weakly typed, Lisp is strongly typed, Lisp is weakly typed, Perl is strongly typed, Perl is weakly typed. (Does that clear up any confusion?)

Everybody has their own definition. Some that I have seen are:

  • strongly typed = statically typed
  • strongly typed = explicitly typed
  • strongly typed = nominally typed
  • strongly typed = typed
  • strongly typed = has no implicit typecasts, only explicit
  • strongly typed = has no typecasts at all
  • strongly typed = what I understand / weakly typed = what I don't understand
  • strongly typed = C++ / weakly typed = everything else
  • strongly typed = Java / weakly typed = everything else
  • strongly typed = .NET / weakly typed = everything else
  • strongly typed = my programming language / weakly typed = your programming language

In Type Theory, there exists the notion of one type system being stronger than another. In particular, if there exists an expression e1 such that it is accepted by a type system T1, but rejected by a type system T2, then T2 is said to be stronger than T1. There are two important things to note here:

  1. this a comparative, not an absolute: there is no strong or weak, only stronger and weaker
  2. there is no value implied by the term; stronger does not mean better (in fact, the "strongest" type system is then the one which simply rejects all programs, which is clearly not useful)

The only definition that I have seen consistently applied is:

  • strongly typed: my programming language
  • weakly typed: your programming language