Scala: generics, primitive types and nulls

Posted in: Technical Track

While working on a Scala (2.12.4) project, I made a typo in my code and received an interesting output:

scala> new Get[Int]().inner() == null
<console>:13: warning: comparing values of types Int and Null
   using `==' will always yield false
       new Get[Int]().inner() == null
res0: Boolean = true

While it was an absolutely useless construction, it still causes such misleading output as seen above:

class Get[T]() {
  val x: T = inner()
  def inner(): T = x
new Get[Int]().inner() == null

I would say that this is a minor Scala bug and probably is a side effect of some of value type + null bugs and other bugs, bugs.

Want to talk with an expert? Schedule a call with our team to get the conversation started.

About the Author

Valentin is a specialist in Big Data and Cloud solutions. He has extensive expertise in Cloudera Hadoop Distribution, Google Cloud Platform and skilled in building scalable performance critical distributed systems and data visualization systems.

No comments

Leave a Reply

Your email address will not be published. Required fields are marked *