I'd say that was an example of strong typing. The + operator in both languages converts types if required. But it's a proper type conversion - it's not reinterpreting the existing data as another type.
If you are happy with a method like Ruby's puts converting objects to a string, why is + any different? They both do type conversion if required.
In my mind an example of a weakly typed language would look like this:
print 1 + "1";
> 399299293
Here my imaginary language has taken the object that represents the string "1", it has ignored the type and has reinterpreted the string object (which happens to be a pointer to a character array) as if it were an integer. This is what C does - which is why it is a weakly typed language in my mind.
C is also a weakly typed language, but it implicitly casts the string object to an integer memory address instead of implicitly parsing the string into an integer like PHP and Perl do.
JavaScript, also being weakly typed, will opt to perform string concatenation, so 1 + "1" = "11".
In strongly typed languages, you can expect an exception rather than an implicit type coercion, when you pass an argument or operand of the wrong type.
But that doesn't rule out that in a strongly typed language like Ruby you can still do something like monkey-patch the String and Fixnum classes to provide custom polymorphic add methods.
>> In strongly typed languages, you can expect an exception rather than an implicit type coercion, when you pass an argument or operand of the wrong type.
Scala and Java's + operator also do implicit conversions to string. Do you think Scala and Java are both weakly typed?
You said that you think Ruby and Python are strongly typed because they don't do these implicit conversions. But they do: Ruby's Kernel#puts and Python's print convert to string.
Also, is 0.5 + 1 ok? One of those operators is a float and the other an integer. In almost every language we get an implicit conversion from integer to float. Does that mean all these languages are weakly typed? Or is conversion to string some kind of exception for some reason?
> JavaScript, also being weakly typed, will opt to perform string concatenation, so 1 + "1" = "11".
How is this an example of weak typing when type information is essential to doing it?
> In strongly typed languages, you can expect an exception rather than an implicit type coercion, when you pass an argument or operand of the wrong type.
That's static typing, which is orthogonal to what we're talking about.
I think your definition is "good", even if it isn't widely-agreed-upon. There may be other "good" definitions, but I don't think the one your parent post is one, because it doesn't seem to permit a "strongly typed" language to define methods that take different types and convert them before operating on them, which would make all languages "weakly typed".
>The + operator in both languages converts types if required.
But that is what weak typing is.
>In my mind an example of a weakly typed language would look like this
Just because it is converting the other way doesn't mean it is a different thing. The fact that it one happens to convert in the direction that pleases you doesn't mean it is "strong" now. If it is converting, then it is weak.
But most languages have some operation that will implicitly convert values to a different type. In fact Ruby and Python both do this (puts and print are examples), and they were examples given of a strongly typed language.
I didn't give those examples. Strong/weak isn't binary, it is a scale. A language that does more implicit conversion is weaker than one that does less.
It's the difference between conversion and casting:
Casting, explicit or implicit, throws away type information, and reinterprets the same data in a different way. Data, here, is bytes, or some other low-level representation that makes what the types apply to.
Conversions, explicit or implicit, uses type information to either change data, or prove that no change is needed, in order to use the data a different way.
But that isn't what we're talking about. Changing a number to a string and using it as a string is exactly as bad as changing a string to a number and using it as a number.
Only if the strong type system is specified to return errors on those kinds of mismatches, as opposed to using the type information to perform a conversion and give another result which is just as well-defined as an error.
That's the point: Strong typing ensures all results are well-defined, as the result of a specification, and it uses type information to do that. Weak typing throws away type information, so it can no longer uphold such specifications.
Python (strongly typed):
Ruby (also strongly typed): PHP (weakly typed): Perl (also weakly typed):