Scala default parameters and null

前端 未结 3 2044
無奈伤痛
無奈伤痛 2020-12-24 06:09

I have a method like this:

def aMethod(param: String = \"asdf\") = {
    ...
}

If the method is called as follows, then param is given the

3条回答
  •  有刺的猬
    2020-12-24 07:07

    If the method has just one or two default parameters that can be set to null consider this pattern:

    // please note that you must specify function return type
    def aMethod (x:String = "asdf"):String = if (x==null) aMethod() else {
        // aMethod body ...
        x 
    }
    

    There are some benefits:

    • The method definition clearly indicates the parameter's default value.
    • The correct default value can be picked by Scala tools, including ScalaDoc.
    • There is no need to define an additional value to substitute for the original parameter within the method body - less room for mistakes, easier to reason.
    • The pattern is fairly concise.

    Furthermore, consider the following scenario:

    trait ATrait {
      def aMethod (x:String = "trait's default value for x"):String
    }
    
    class AClass extends ATrait {
        ....
    }
    

    Clearly, here we need to extend the trait, whilst preserving the original default value. Any of the patterns that involve initially setting the parameter to null followed by a check and actual default value will break the contract established by the trait:

    class AClass extends ATrait {
      // wrong, breaks the expected contract
      def aMethod(x: String = null):String = {
          val xVal = if (x == null) "asdf" else x 
          ...
      }
    }
    

    Indeed in this scenario the only way to preserve the original value from ATrait will be:

    class AClass extends ATrait {
      override def aMethod (x:String):String = if (x==null) aMethod() else {
        ... // x contains default value defined within ATrait
      }
    }
    

    However, in the scenario when there are more than one or two default parameters that can be set to null the pattern starts getting rather messy:

    // two parameters
    def aMethod (x:String = "Hello",y:String = "World"):String = 
      if (x==null) aMethod(y=y) else
      if (y==null) aMethod(x=x) else {
        // aMethod body ...
        x + " " + y
    }
    
    // three parameters
    def aMethod (x:String = "Hello",y:String = " ",z:String = "World"):String = 
      if (x==null) aMethod(y=y,z=z) else
      if (y==null) aMethod(x=x,z=z) else 
      if (z==null) aMethod(x=x,y=y) else {
        // aMethod body ...
        x + y + z
    }
    

    Still when overriding an existing contract this might be the only way to honour the original default values.

提交回复
热议问题