2009/3/24 Jorge Ortiz <
jorge.ortiz@...>:
> This sounds dangerously close to some dangerous monkey-patching.
>
> package java.lang
>
> object String {
> def new() = ... // I'm in ur compiler, hackin' ur stringz
> }
This doesn't work. It has to go on the companion object, which has to
be defined in the same file. It's not enough for the object to merely
have the same fully qualified name. Also the above simply gives a
compile error because the object is defined twice.
> Maybe String is a bad example because you rarely use "new String"
> explicitly, but you get the idea.
>
> Also, I think this lends itself to more WTFs/minute
> (
http://www.osnews.com/story/19266/WTFs_m) for people unfamiliar with the
> feature.
>
> Consider:
>
> "Someone is calling this constructor, but I don't see it defined anywhere.
> WTF?"
>
> vs.
>
> "I just added a new constructor to this class, but my code is mysteriously
> doing something else instead. WTF? WTF!"
>
> The former might lead me to seek out someone more knowledgeable with Scala,
> while the latter might lead me to bang at the code for three hours in an
> attempt to figure it out.
Fair point. It's probably a bad idea. :-)