How to check for null or false in Scala concisely?

后端 未结 7 1554
时光取名叫无心
时光取名叫无心 2020-12-24 06:23

In Groovy language, it is very simple to check for null or false like:

groovy code:

def some = getSomething()
if(some) {
//         


        
7条回答
  •  滥情空心
    2020-12-24 06:58

    If you use extempore's null-safe coalescing operator, then you could write your str example as

    val str = ?:(some)(_.toString)()
    

    It also allows you to chain without worrying about nulls (thus "coalescing"):

    val c = ?:(some)(_.toString)(_.length)()
    

    Of course, this answer only addresses the second part of your question.

提交回复
热议问题