Given a method like…
def myFun(val) puts(val+3) end
If I accidentally call it with the String “5″, I get a type error, which looks like this:
test.rb:2:in `+': can't convert Fixnum into String (TypeError) from test.rb:2:in `myFun' from test.rb:4
That’s fine by me, but now I’d like to give a more meaningful error message (“Hey, buddy, you meant to pass in something that can +3. Like a Vorpal Sword.”). At that point, though, I’m basically hand-rolling my own static typing, except without all the useful compile-time enforcement or automatic checking.
Note the corresponding Perl subroutine Does The Right Thing:
sub my_fun($) { print ($_[0] + 3) } my_fun("5")
And I don’t mind that. If you’re going to play it fast-and-loose with types, that really is fine by me. But Ruby gets on my nerves with the way it does not let me do typing when useful and yet eagerly punishes me for using the wrong types. If a language is going to give me the responsibility of handling types, give me the power to handle them well. If it’s going to take responsibility for handling types, then it can have the power. There are domains where each of these is an appropriate take on things. But giving me the responsibility while not giving me the power simply irks me.
Related posts:
Pingback: More Ruby Type Weirdness | Enfranchised Mind
Pingback: Ruby is the Future | Enfranchised Mind