On Sun, 18 Dec 2011 13:45:35 +1100, Chris Angelico wrote: > On Sun, Dec 18, 2011 at 11:59 AM, Steven D'Aprano > <steve+comp.lang.pyt...@pearwood.info> wrote: >> The usual test of a weakly-typed language is that "1"+1 succeeds (and >> usually gives 2), as in Perl but not Python. I believe you are >> confusing weak typing with dynamic typing, a common mistake. > > I'd go stronger than "usually" there. If "1"+1 results in "11", then > that's not weak typing but rather a convenient syntax for > stringification - if every object can (or must) provide a to-string > method, and concatenating anything to a string causes it to be > stringified, then it's still strongly typed.
For what it's worth, Wikipedia's article on type systems gives Javascript as an example of weak typing because it converts "1"+1 to "11". I agree with them. http://en.wikipedia.org/wiki/Type_system I think that weak and strong typing aren't dichotomies, but extremes in a continuum. Assembly languages are entirely weak, since everything is bytes and there are no types to check; some academic languages may be entire strong; but most real-world languages include elements of both. Most commonly coercing ints to floats. Chris Smith's influence article "What To Know Before Debating Type Systems" goes further, suggesting that weak and strong typing are meaningless terms. I don't go that far, but you should read his article: http://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/ > Or is a rich set of automated type-conversion functions evidence of weak > typing? And if so, then where is the line drawn - is upcasting of int to > float weak? To my mind, the distinction that should be drawn is that if two types are in some sense the same *kind* of thing, then automatic conversions or coercions are weak evidence of weak typing. Since we consider both ints and floats to be kinds of numbers, mixed int/float arithmetic is not a good example of weak typing. But since numbers and strings are quite different kinds of things, mixed str/int operations is a good example of weak typing. But not *entirely* different: numbers can be considered strings of digits; and non-digit strings can have numeric values. I don't know of any language that allows 1 + "one" to return 2, but such a thing wouldn't be impossible. -- Steven -- http://mail.python.org/mailman/listinfo/python-list