Working Effortlessly with Java null in Scala


We would not repeat why null is a billion dollar mistake of Java. In our earlier post, we have already discussed how to handle nulls of Java. In this post we would take it a step further, but first a bit of recap.

Say that you have a function in Java which can potentially throw a null.

def getUser:User = null

Now, the way we have been handling this in Scala has been with fold. So what we would do is

Option(getUser).fold("Sky")(u=>processUser(u))
def processUser(u:User):String= u.name 

Hence, if the Option(getUser) results in None then we give the default value which is “Sky” else if it is Some then we pass it to a method called processUser which brings us back the name. Hence it is of the type User=>String

Now what is the problem with this approach, actually none but it just leads to quite some boilerplating if we are working with quite a few Java methods which can return null because everytime we would be writing statements like

Option(getUser).fold(“Sky”)(u=>processUser(u))

Let us get it into a common object.

 object SValue {
    def apply[T, P](a: T, f: T => P)(implicit defaultValue: P) = Option(a).fold(defaultValue)(f)
  }

and now how we would use it for the above scenario

SValue(getUser,processUser)("Sky")

No folds, no options and just SValue (Scala Value). You can pass the defautValue explicitly or you can let it be picked up from the implicit scope. The gist for the code is present here.

Ok, so far so good. But what if we would like to defer the function for later. We want to do currying it. How do we do that

object SValue {
    def apply[T, P ](a: T,defaultValue: P)( f: T => P) = Option(a).fold(defaultValue)(f)
}

Now, how do we call it this time. We know that if the value is null what would be the default vlaue but if the value is not null then we would like to apply a function to process later.

val f = SValue1(getUser,"Sky") _   

This gives us f which is a partially applied function. Now how do we apply it later

f(processUser)
f(_.name)   

If you notice, the beauty of the above code is now that Scala is Type inferring our datatype and hence we are just able to call _.name on it. Enjoy, the gist is here

Advertisements

About Satendra Kumar

Satendra kumar is the Senior Associate @ Sapient Global Market. In his 5 years of experience he has become developer with proven experience in architecting and developing the Reactive Systems and Big Data Platform with Scala, Akka, Spark, Kafka & Hadoop. He is the big fan of Scala. Satendra has a B.Tech in Computer Engineering from Uttar Pradesh Technical University.
This entry was posted in Best Practices and tagged . Bookmark the permalink.

2 Responses to Working Effortlessly with Java null in Scala

  1. Reblogged this on Play!ng with Scala and commented:

    Java null in Scala

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s