【问题标题】:Scala - wrapping functions with default parameter valuesScala - 使用默认参数值包装函数
【发布时间】:2017-09-18 00:01:48
【问题描述】:

我正在使用一个库函数,它使用参数 minPartitions 的默认参数值。我有一个包装函数,我从中调用这个库函数。我希望我的包装函数工作的方式是 - 如果为 minPartitions 传递了一个值,我将在调用函数时使用这个值。否则,我将使用默认值而不传递参数。我如何在 Scala 中做到这一点?

def read(foo: String, minPartitions: Integer = ????): RDD[String] = {
  val rdd = sc.hadoopRDD(jobConf,
            classOf[InputFormat],
            classOf[BytesWritable],
            classOf[BytesWritable],
            minPartitions // optional - default value will be used
        ) 
}

【问题讨论】:

    标签: scala overriding default-parameters


    【解决方案1】:

    您有多种选择:

    1. 重用默认值。即将 minPartitions= 设置为与 hadoopRDD 定义中相同的值
    2. 将 minPartitions 设为 Option[Integer],然后您就知道它何时被定义,何时未被定义。
    3. 制作两个版本的函数。

    如果您有自己的逻辑默认值,我会选择选项 1,或者并不真正关心默认值是否会在未来的某个时间发生变化,否则使用选项 2。

    【讨论】:

      【解决方案2】:

      Scala 具有Option 的概念,用于表示可能不存在的值。

      Option[Int] 可以是Some[Int]None。您可以在包装函数中使用它。

      另外...在使用 Scala 时,除非明确要求,否则请使用 Int 而不是 Integer

      现在...一种方法是使用模式匹配

      def read(foo: String, partitionsOpt: Option[Int]): RDD[String] = {
        partitionsOpt match {
          case Some(partitions) => sc.hadoopRDD(
              jobConf,
              classOf[InputFormat],
              classOf[BytesWritable],
              classOf[BytesWritable],
              partitions
          )
          case None => sc.hadoopRDD(
              jobConf,
              classOf[InputFormat],
              classOf[BytesWritable],
              classOf[BytesWritable]
          )
      }
      

      另一个是mapOption然后做一个getOrElse

      def read(foo: String, partitionsOpt: Option[Int]): RDD[String] = {
        partitionsOpt
          .map(partitions => sc.hadoopRDD(
              jobConf,
              classOf[InputFormat],
              classOf[BytesWritable],
              classOf[BytesWritable],
              partitions
          ))
          .getOrElse(sc.hadoopRDD(
              jobConf,
              classOf[InputFormat],
              classOf[BytesWritable],
              classOf[BytesWritable]
          ))
      }
      

      【讨论】:

      • 为方便起见,可能用def read(foo: String, partitions: Int) = read(foo, Some(partitions))def read(foo: String) = read(foo, None) 重载它,让它感觉更像是调用者的“默认参数”。
      【解决方案3】:

      示例函数:

      //you can directly define your default in the parameter list
      def read(minPartitions: Integer = 123): Unit {
          println(minPartitions)
      }
      
      read(77) //prints out 77
      read() //prints out 123
      

      或者,您可以使用另一个答案中提到的 Option/Some/None。

      示例函数:

      //in this function, you must provide an Option value to second parameter
      def read(minPartitions: Option[Int]): Unit {
          println(minPartitions.getOrElse(123))
      }
      
      read(Some(77)) //prints out 77
      read(None) //prints out 123
      

      也可以将这两个概念一起使用(将参数类型定义为Option,并在参数列表中提供默认值)

      希望这会有所帮助! :)

      【讨论】:

        猜你喜欢
        • 2021-11-15
        • 2013-10-13
        • 2017-11-29
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 2011-02-20
        相关资源
        最近更新 更多