Type Safety in a Loosely Coupled World
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');
}
}
More on this subject can be found in Dealing with Diversity.
I'm guessing that Java and C# print out "12", C gives a compiler error and Perl gives 3.
It probably would take me five minutes to check this each compiler/interpreter but I think the above should be accurate. :)
Posted by Dare Obasanjo at
Dare: you got Java, C#, and C wrong, but Perl right. The answers for C and Perl were only one click away... and I even provided the link. ;-)
Posted by Sam Ruby at
chars not strings. Duh.
You know you've been working with XML to much when you start thinking that ' and " are semantically identical just different lexically.
I'll just salve my ego myself with the knowledge that the answers would have been correct if the question was "1" + "2". :)
Posted by Dare Obasanjo at
An interesting contrast is:
1 + "2"
and
"3" + 4
and how different languages treat them.
Perl always uses numeric addition for '+' and uses a different operator ('.') for string concatenation. (Similarly for numeric multiplication '*' and string repetition 'x'.)
Posted by Ken MacLeod at
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.)
Posted by Mark at
VBScript results:
WScript.Echo '1' + '2'
=> ""
WScript.Echo 1 + '2'
=> compilation error
Both of these answers I find a bit surprising.
And Mark, the interesting question is whether you picked the language based on the way you think, or if the language has influenced the way you think.
Posted by Sam Ruby at
Sam, to your question to Mark, I designed the language to fit the way my mind worked. ;->
Posted by Dave Winer at
Sam, the results of VBScript are a consequence of the single quote being a line comment.
Anything after the "'" is a comment.
Therefore
WScript.Echo '1' + '2'
is equivalent to
WScript.Echo
and
WScript.Echo 1 + '2'
is equivalent to
WScript.Echo 1 +
Posted by Jeremy at
DOH! Thanks Jeremy.
I was trying hard to keep it to '1'+'2' as introducing double quotes changes the semantics in a number of languages, but in VBScript's case:
"1" + "2" = "12"
"1" + 2 = 3
Posted by Sam Ruby at
Sam and Dave: I felt that Python fit my brain the very first weekend I learned it. It was love at first byte.
Posted by Mark at
Not related, but fun to read, Sam Ruby posts Type Safety, in a Loosely Coupled World.
Seen on paradox1x: Teaching Java the Extreme Way at
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
+ anotherString
self, anotherString.
and done
Posted by James Robertson at
In PHP, '1' + '2' is 3.
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.
FWIW, '1' + 2 and 1+ '2' both equal 3, as well.
Posted by Adam Trachtenberg at
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?
Posted by josh at
I Ruby it's the 'short form' for:
1.+(2)
which yields 3.
That's why I love Ruby. Everything is an object, even when it doesn't look like it.
Posted by Chris Gehlker at
Josh: Right. In C there is no "render yourself as a string" method, so you have to improvise.
Chris: In Ruby, "print '1' + '2'" yields "12".
Posted by Sam Ruby at
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.
Posted by Lach at
In plain C the answer is 99:
#include <stdio.h>
#include <stdlib.h>
int main(void){
printf("res: %c (%d)\n", ('1' + '2'), ('1' + '2'));
return 0;
}
-------------
[skitzo:~] ed% cc -g add.c
[skitzo:~] ed% ./a.out
res: c (99)
Posted by Ed Silva at
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.
Posted by josh at
Josh: Try the following, and tell me whether '1'+'2' is the same as 'c':
public class test {
public static void main(String args[]) {
System.out.println('c');
System.out.println("99");
System.out.println('1' + '2');
}
}
Posted by Sam Ruby at
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'.
Posted by josh at
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:
System.out.println(99);
Posted by Sam Ruby at
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.
Posted by josh at
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.
Posted by Allan at
Lach --
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.
Posted by Adam Trachtenberg at
Adam, I mentioned numeric addition (+) vs. concatenation (.) in Perl earlier.
In the type safety/loose coupling sense, this design decision moves the assumption of type further into run-time.
Posted by Ken MacLeod at
Allan, the VisualWorks 7 Smalltalk environment raises an exception for
'1' + '2', same for Dolphin Smalltalk 5.0.
Posted by Nils Kassube at
Radio thinks it's "12".
'1' is a character constant as is '2'.
Add two character constants, you get a string constant, which is the concatenation of the two chars.
Posted by Dave Winer at