Can I reproduce Scala's behavior for == ?
- by JPP
In Programming in Scala, I can read that the == operator behaves as if it was defined like this:
final def == (that: Any): Boolean = if (null eq this) {null eq that} else {this equals that}
But there must actually be compiler magic to avoid null pointer exceptions, right? Is there any way for me to replicate this behavior with pure Scala; i.e., have an operator/method return one thing if the receiver is null and another one if it isn't? What I mean is an actual implementation of null eq this.
I suppose I can write a "pimp" and then define the method on the wrapper class, but is there a more direct way to do this?