简体   繁体   中英

Java/Scala project : checking null vs. matching

i am working on a Java/Scala project and right now and in my Scala class I am checking to see if a java object's method will return a string or null. What is the best way to do this check? Should i continue to just check if the method returns null like someone would do in Java or should i be wrapping it in an Option and be using Scala's matching of Options. Thanks in advanced!

In Scala, you should use Option[String] to make it clear:

val stringOpt: Option[String] = Option(someJavaMethodThatReturnsNull())

And then use pattern matching or the functions of Option .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM