Scala avoid using null

前端 未结 5 2005
情深已故
情深已故 2021-01-18 04:07

I hava a project on github that is analysed by codacy . The analysis suggest to \"Avoid using null\" for the following line of code:

def doS         


        
5条回答
  •  猫巷女王i
    2021-01-18 04:11

    There's no need to explicitly check for null or wrap path in an Option. You can do this:

     path match {
      case p: String => Option(doSomethingWith(p))
      case _         => None //if path is null this will be returned
     }
    

    That will return an Option, which may not be what you want, but in that case instead of producing a None, raise an exception. require will raise an exception in your example anyway so if that's what your caller expects just do it explicitly.

提交回复
热议问题