Brian Candler
5/26/2007 6:35:00 PM
On Sun, May 27, 2007 at 01:00:03AM +0900, Henrik Schmidt wrote:
> Hakusa@gmail.com wrote:
> >If a language stops you from doing something just because it's bad
> >practice, then the language is treating you like an idiot.
>
> Fine, so don't stop be. Warn me that I'm doing something which is
> probably a programmer error 99% of the time. Ruby stops me from all
> sorts of things I can do in Perl. I think that's a good thing, since I'm
> a horrible Perl programmer :)
>
> >What if I
> >wanted to override a function for a little while? I could assign it a
> >new value and use the same methods of a different class!
> >
>
> Then you'll just override it. My question is, why would you want to
> override a method with a variable? I have no problem with overloading a
> method with another method, and neither should the interpreter.
The trouble is:
(1) All objects are descendants of Object, which in turn mixes in Kernel.
Many other objects mix in Enumerable, Comparable and other modules.
This means that a typical object has roughly a googol different methods
already present, and it's very easy to pick a local variable name which
happens to collide with one. Silently ignoring this "just works". Giving an
error would be very annoying; instead of "id = 9" I'd have to change it to
"my_id = 9" or somesuch. In the end I'd prefix all local variables with
my_... which would be worse than using something perlish like "$"
(2) From a very practical perspective, it's extremely difficult for Ruby to
generate these warnings.
The problem is: Ruby is a completely dynamic language, and at parse time you
have no idea what methods an object has. The decision as to 'local variable'
or 'method' is made statically, based on inspection of the code
*before* it's executed, which means before any classes and methods have been
created.
To perform the check you're asking for, Ruby would have to add extra
run-time code after *every* local variable access to perform a method search
just to check if a method with the same name exists. Example:
10000.times do
x = flurble()
y = y + x
end
Each time round the loop, the call to flurble() may have ended up defining a
method called 'x' in the current object. So every time round the loop, you'd
have to check, at the point where 'x' was read and/or assigned, that there
was currently no method called 'x' (or 'x=') in the object.
Regards,
Brian.