4 ms·
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 integ
by kyllo 12y ago
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.
- chrisseaton 12y ago>> 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?
- cbd1984 12y ago> 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.