If I want to add a method to a class in Scala, I need to do something like:
class RichFoo(f: Foo) {
def newMethod = f.bar()
}
object RichFoo {
implicit def foo2Rich(f: Foo) = new RichFoo(f)
}
Then f.netMethod will result in creation of RichFoo instance and call its method.
I'm trying to understand why it was not defined similarly to Ruby:
override class Foo {
def newMethod = bar
}
The compiler can look at this definition, and create a FooOverride class with static method newMethod that gets a parameter of type Foo and calls its bar method. This is how Scala implements traits. I still need to import the package containing the Foo override to use it.
It seems to involve less typing, doesn't require me to invent names, and has better performance (not calling a method and creating an object). Anything the implicit conversion method does can be done inside the additional method.
I'm sure I've missed something and would like to get an insight into what.