Skip to content

Instantly share code, notes, and snippets.

@satendrakumar
Last active April 15, 2017 04:32
Show Gist options
  • Save satendrakumar/ca79097d777ac87db14c to your computer and use it in GitHub Desktop.
Save satendrakumar/ca79097d777ac87db14c to your computer and use it in GitHub Desktop.
Handling Java null with Scala
object play {
println("Welcome to the Scala worksheet")
case class User(id:Int, name:String)
def getUser:User = null
def processUser(u:User):String= u.name
Option(getUser)
Option(getUser).fold("Sky")(u=>processUser(u))
object SValue {
def apply[T, P](a: T, f: T => P)(implicit defaultValue: P) = Option(a).fold(defaultValue)(f)
}
SValue(getUser,processUser)("Sky")
object SValue1 {
def apply[T, P ](a: T,defaultValue: P)( f: T => P) = Option(a).fold(defaultValue)(f)
}
val f = SValue1(getUser,"Sky") _
f(processUser)
f(_.name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment