Chris Sells: I have always turned up my nose at run-time
type checking until I realized (and this is the insight I got from
Tim's talk) that *all* marshaling-based type checking is done at
run-time. Excellent!
Now for fun: what is '1' + '2'? The answer may surprise
you. C# and Java agree (insert obvious joke here), but get a
different answer than C, Perl, or Python do (which get three
different answers). Here's some test
programs.
C#
public class test {
public static void Main(string[] args) {
System.Console.WriteLine('1' + '2');
}
}
Java
public class test {
public static void main(String[] args) {
System.out.println('1' + '2');
}
}
Perl always uses numeric addition for '+' and uses a different operator ('.') for string concatenation. (Similarly for numeric multiplication '*' and string repetition 'x'.)
Python's answer makes the most sense to me, which is probably why I spend as much time as possible programming in Python. ("It fits my brain.")
Another interesting test might be 1 + '2'. Add a few more languages into the mix, like VBScript or VB.NET. (I honestly don't know what the result would be. I believe in VBScript the result is 3, but it's been a while and I could be wrong.)
In Smalltalk, a language many falsely claim is not type safe, you'll get a nice exception for
'1' + '2'
Why? Because '1' is an instance of string, and does not understand the message #+. Now, if you wanted to extend class String to understand #+ (say, to make #+ work the same way #, works, concatenation), you could add this method to class String
Since PHP was created as a form interpreter (the FI in PHP/FI), it's very loose. It tries really hard to guess a variable's type because it's difficult for the programmer to specify them either in the code or in HTML inputs and want to make this as easy as possible.
I may be looking at this differently, but it seems to me that C and Java (and I guess C#) all got the same answer. The catch is that in C the putc command then cast the result back to a character. In Java it left it as the integral type that + operated on. Right?
Adam, the reason it works that way is of course because + is for numeric addition in PHP as opposed to . being for string concatenation, so if you're using + PHP is always going to try to treat them as numbers, and add them.
I guess I was simply pointing out that Java and C both had the same answer. The catch was that Java did not recast it to a char. Due to the overloading on println(), the method that takes in an int is called. If you force it to use the one that takes a char, you get 'c' just as in C. Note, the distinction being that NO method is called on the character or int; instead, just as in C, the method used to print the result caused the change.
i.e. System.out.println( (char) ('1' + '2') );
I still like the problem. Though, it does make me more leary (I think that is the word) of operator overloading.
My point is that you are calling three seperate methods there. In one, you are calling println that takes in a char, another a String, and the last an int.
as far as the expression '1' + '2' is conserned they DO get the same answer, but you printed it out as an int in the java code.
As an example, try char x = '1' + '2'; System.out.println(x);
You are still right in that it is the polymorphism that is changing it to 99. I just was pointing out that before the call to println(int), they had the same answer.So, if this came up in an assignment and not straight in the println (as my example) you would get 'c'.
Josh, it is a very subtle point, but I have tried to be consistent with my use of quotes. Look in Dealing with Diversity, the three answers I gave were 'c', 3, and "12".
When you add in Java/C#, you get a fourth answer: 99. 99 can be explicitly converted to 'c', but is distinguishable from that value.
This being said, I could have been clearer in the example above; I should have said:
Why do I feel like I am being a bother? :) I apologize for being incredibly dense in my posts. (I'm not sure dense is the correct word, but it will have to do.)
I am honestly not trying to be difficult.
I am just saying that the expression "'1' + '2'" is the same in C and Java. Now, I fully agree that a likely use (that is, just printing the answer) is different. However, in a different context (in the same respective languages) the answer is the same. That is one of the reasons I do not like operator overloading overly much. Especially since it means "" + '1' + '2' is different from '1' + '2' + "".
Am I making a valid point, or just blowing hot air? To reiterate, the method putchar is similar (to me) to System.out.println(char), so if you did System.out.println((char)99), you get the same answer again.
James Robertson, what Smalltalk dialect are you using? In my Squeak image, doing a printString on '1' + '2' gives me '3'. Diving into the browser, I see #+ uses a double dispatch on the argument '2' which coerces it into a number and sends #+ with the caller.
While your comment about + always being addition compared to . for string concatenation is true, this is identical to how Perl operates -- and I don't see anyone bringing this up for that language. :)
But you're right, so I went back and tested this code under PHP 3, where + was overloaded to function for both strings and numbers -- a better test because it's more C-like.
Anyway, under PHP 3, '1' + '2' still returns 3. Again, it stems from PHP not knowing whether form input is "supposed" to be strings or numbers, so it wants to be "friendly." Since I believe this is a specific design decision of the language, and not an accident, I think it's an interesting comparison to the others, where this was disregarded.