Last active
April 15, 2017 04:32
-
-
Save satendrakumar/ca79097d777ac87db14c to your computer and use it in GitHub Desktop.
Handling Java null with Scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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