What is a strongly typed language? Are dynamic languages like Python and Ruby automatically weakly typed? Is strong just another word for static and weak just another word for dynamic? I’m lost… help me Fleebie!
#1 Dynamic Vs Static
First step to avoid confusion : Don’t mix Dynamic/Static typing with Strong/Weak typing. Ruby, for example, is dynamically and strongly typed at the same time (more on that later).
Example of a language statically typed :
int x;
x = 3;
x = "hello"; //ERROR!
Example of a language dynamically typed :
x = 3
x = "Transformers seems to be a boring movie"
x = :why_are_we_here
x = /is this a reg ex/
x = "enough already... I think we understand"
When we talk about dynamic typing, we talk about a mechanism where the type of a variable can change and be resolved on the fly at the exact moment it gets parsed by the interpreter.
When we talk about static typing, we talk about a mechanism where the type of a variable is resolved in advance by the interpreter/compiler. With statically typed languages, you cannot say that x is a string and, a few lines after, that it is an integer. That’s all there is to say about it.
As you already know, Ruby is dynamically typed. Javascript falls in the same category as well.
#2 Strong Vs Weak
Question : Why Ruby is a strongly typed language?
Answer : Because once it knows the type of an object, it expects you to do something that makes sense with it.
In ruby, you CAN do :
x = "3"
y = x + "ho!" #result : "3ho!"
but you CANNOT do :
x = "3"
y = x + 3 #Wooo! Ruby won't like that
What do you want to do with the string “3” ? Append the Fixnum 3? What? Are you crazy? Hey look everyone, that guy want to append the string “3” to the Fixnum 3! Let’s make fun at him!
(Python and PHP are looking at each other and Python says : You knew that Ruby was still on medication?)…
Oh sh**! My geek meter just exploded…
You see, It’s not because Ruby lets you change the type of an object as many times as you want that it doesn’t care about what you do with it. If you start mixing several types together in an expression, it won’t try to understand what it means, nah… that is too complicated. Instead, ruby will throw an exception… end of story. This is why we say that Ruby is a strongly typed language.
Javascript however is dynamically but weakly typed because it lets you mix types together in your expressions without throwing an exception. The danger? If you’re not careful you will get weird results.
x = call_function_that_returns_the_number_3_in_the_string_form;
y = x + 1 //The programmer expect that "y" will equal 4 (Tee hee hee!)
after that instruction, y will contain “31”. Hmm, that could be a bug hard to spot. Javascript is being more hippie than Ruby on this level. It’s like : relax man… do watcha want with your types, I don’t care. I’ll mix them all and we will have a good laugh watching the results. It will be like “art”! For this reason, Javascript is a weakly typed language.
So, to conclude :
A dynamically typed language is a language where the type of a variable can be altered at any time. (It is a string, now it is a Fixnum, now it is a Time object, etc.)
A statically typed language is the opposite. (Decide what x is once for all and don’t change your mind!)
A strongly typed language is a language that is being strict about what you can do with your typed variables. (Don’t mix them… or I will throw you an error in the face!)
A weakly typed language is the opposite. (Do what you want with your different types. Mix them all! We’ll see what happens!)