Does weak typing offer any advantages?

Posted by sub on Stack Overflow See other posts from Stack Overflow or by sub
Published on 2010-04-02T14:47:39Z Indexed on 2010/04/02 14:53 UTC
Read the original article Hit count: 480

Don't confuse this with static vs. dynamic typing!

You all know JavaScripts/PHPs infamous type systems:

PHP example:

echo "123abc"+2; // 125 - the reason for this is explained
                 // in the PHP docs but still: This hurts

echo "4"+1;      // 5 - Oh please

echo "ABC"*5;    // 0 - WTF
                 // That's too much, seriously now.
                 // This here might be actually a use for weak typing, but no - 
                 // it has to output garbage.

JavaScript example:

// A good old JavaScript, maybe you'll do better?


alert("4"+1);    // 51 - Oh come on.

alert("abc"*3);  // NaN - What the...
                 // Have your creators ever heard of the word "consistence"?

Python example:

# Python's type system is actually a mix
# It spits errors on senseless things like the first example below AND
# allows intelligent actions like the second example.
>>> print("abc"+1)
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    print("abc"+1)
TypeError: Can't convert 'int' object to str implicitly
>>> print("abc"*5)
abcabcabcabcabc

Ruby example:

puts 4+"1"         // Type error - as supposed
puts "abc"*4       // abcabcabcabc - makes sense

After these examples it should be clear that PHP/JavaScript probably have the most inconsistent type systems out there. This is a fact and really not subjective.

Now when having a closer look at the type systems of Ruby and Python it seems like they are having a much more intelligent and consistent type system.

I think these examples weren't really necessary as we all know that PHP/JavaScript have a weak and Python/Ruby have a strong type system. I just wanted to mention why I'm asking this.

Now I have two questions:

  • When looking at those examples, what are the advantages of PHPs and JavaScripts type systems? I can only find downsides:

    • They are inconsistent and I think we know that this is not good
    • Types conversions are hardly controllable
    • Bugs are more likely to happen and much harder to spot
  • Do you prefer one of the both systems? Why?

Personally I have worked with PHP, JavaScript and Python so far and must say that Pythons type system has really only advantages over PHPs and JavaScripts.

  • Does anybody here not think so? Why then?

© Stack Overflow or respective owner

Related posts about type-conversion

Related posts about strong-typing