Can I reproduce Scala's behavior for == ?
Posted
by
JPP
on Stack Overflow
See other posts from Stack Overflow
or by JPP
Published on 2011-01-31T18:26:31Z
Indexed on
2011/02/01
7:25 UTC
Read the original article
Hit count: 162
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?
© Stack Overflow or respective owner