On the subject of typing
I often hear the argument "Language X is/isn't a strongly typed language because Y". Often times this argument arises because the language is dynamically typed, and therefore it must not be static. Except that most dynamic languages today are strongly typed, even if it may not seem immediately obvious.
I often hear Python being cited as a weakly typed language because its function arguments don't require typing, or because you can assign a different type of value to the same variable name. One thing those people don't mention, is this:
>>> "hello" + 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't convert 'int' object to str implicitly
You actually need to manually convert the integer to a string before concatenating or else you'll get a type error. Weakly typed languages don't give you type errors.
Ruby does the exact same thing:
irb(main):001:0> "hello" + 3 TypeError: no implicit conversion of Fixnum into String from (irb):1:in `+' from (irb):1 from /usr/bin/irb:12:in `<main>'
And so does Common Lisp:
[1]> (+ "hello" 3) *** - +: "hello" is not a number
JavaScript by comparison just doesn't give a damn:
> "hello" + 3 'hello3'
One could argue that JS is strongly typed, it just has type coercion that converts things into the appropriate types before attempting to do an operation on them, which I guess is a fair point; after all the above example simply converts the number to a string before concatenating it with "hello". I mean both C# and Java do the same thing, they both implicitly call .toString() on an object if its string is needed, and nobody calls those languages "weakly typed".
What about PHP?
> "2" + 2; => 4
I don't know about you, but I don't think that's supposed to happen. And yet it does, because PHP isn't strongly typed. I have a feeling it's because everything in PHP is represented as a string internally to ease the burden of doing web-development. That also comes with the gotcha that any representation of a number, string or otherwise, is a legal number to do maths with.
That said, not just dynamically typed languages are weakly typed, a few static languages are too.
If you know C well enough, you'll soon realise that pretty much everything is a number internally, which can lead to some pretty freaky stuff if you do it with care.
Note the following example (C doesn't have an interactive mode like the others):
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *str = "Hello, World!"; // set target string char *current = str; // set head of pointer to beginning while (*current != 0) { // while the string isn't 0 printf("%c\n", *current); // print the string pointer as a char current = current + 1; // add 1 to the string } return EXIT_SUCCESS; }
What's gong on here, is that I'm treating the string pointer current like an integer, adding 1 to it every time I've printed it. And since I know a string ends in a '\0' in C, '\0' having the ascii value 0, I can just check for it as such. And for those not familiar, C doesn't have strings, it instead uses char arrays and/or char pointers for that.
"But that's just pointer manipulation" I hear you cry. I know! That's my point, you can do this with structs and unions as well if you know your way around their internal memory representation. That's why I'd argue that C isn't strongly typed. C essentially allows you to add a struct to a string and get out the sum of their memory locations; and if you're going by the principle of least astonishment, then that's definitely not what you'd expect would happen.
In short:
Strong typing: Obeys type rules, types can't be mixed implicitly
Examples: Erlang, Python, Ruby, Common Lisp, Haskell, OCaml
Weak typing: Ignores type rules, types can be mixed implicitly
Examples: PHP, C, JavaScript
Static typing: Explicitly stating the types, variables can't change their type
Examples: C, C++, Haskell, C#, Java, Scala, OCaml
Dynamic typing: Types not stated, variables can change their types
Erlang, Python, Ruby, Common Lisp, Scheme
















