Can I reproduce Scala behavior for ==?

In programming in Scala, I can read that the == operator behaves as if it were defined as follows:

 final def == (that: Any): Boolean = if (null eq this) {null eq that} else {this equals that} 

But there really needs to be compiler magic to avoid exceptions from null pointers, right? Is there a way to reproduce this behavior using pure Scala; that is, the operator / method returns one thing if the receiver is null and the other if not? I mean the actual implementation of null eq this .

I suppose I can write a “pimp” and then define a method in a wrapper class, but is there a more direct way to do this?

+4
source share
6 answers

I do not think so. As far as I know, there is no magic for zeros. (see Update)

I think the best thing you can do is wrap any object in a parameter so that you can use a bunch of useful things from it:

 implicit def toOption[T](target: T) = Option(target) val q: String = null val q1: String = "string" println(q getOrElse "null") // prints: null println(q1 getOrElse "null") // prints: string 

Update

I found this document:

http://www.scala-lang.org/api/2.7.7/scala/Null.html

In accordance with this:

The Null class - along with the Nothing class - is at the bottom of the Scala type hierarchy.

That way, even null has methods inherited from AnyRef , such as eq , == , etc .... And you can also use them:

 val q: String = null val q1: String = "string" println(null eq q) // prints: true println(null eq q1) // prints: false 
+2
source

"null" is the only instance of the trait named Null - so this is a regular object, not magic for calling ==

You should definitely check the option and do everything possible to save zeros from the code :)

+2
source

There is no magic for zeros, but scala has some magic to ensure equality, symmetrical for numbers. The trick is to extend the ScalaNumber ...

http://www.scala-lang.org/node/6387

Update

Just to clarify that bit ... This means that if you write a == b and b comes from ScalaNumber or (I believe) it is AnyVal , then the compiler will test b == a instead.

This not only eliminates the null situation, but also makes it easier if you want to compare the primitive with some other type, which can be considered as a number, but for which the implicit conversion will be recklessly unsafe. This is the approach used, for example, by BigInteger.

+2
source

There seems to be no way to do this without a special case with a zero case. Java will not allow a method to be called if the object is null. This is compared to a language like python, where None is a suitable object. I think it's best to try to ignore the fact that null exists.

Using Implicit to Simulate ==

 class Equals2(v:AnyRef){ def ===(that:AnyRef) = if(v eq null) {that eq null }else {v equals that} } implicit def equals2(v:AnyRef) = new Equals2(v) 

Unfortunately, the following does not work, since null is not a subclass of AnyRef

 null === "Something" 
+2
source

null is the only null instance and therefore, in Scala, is a complete object.

If you define your function in the spirit, as you indicated above in class A , all you have to do is provide an implicit conversion from null to A that maps null to some dummy instance.

+1
source

You can use the option.

 scala> Option("a") res0: Option[java.lang.String] = Some(a) scala> Option(null) res1: Option[Null] = None 
0
source

Source: https://habr.com/ru/post/1337824/


All Articles