在 Scala 中设置函数参数的默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19090103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 05:43:41  来源:igfitidea点击:

Set default value for function parameter in scala

scalalambda

提问by user809564

I am trying to set a default value to an anonymous function in scala and so for not able to find any solution. Hope some one would help me out in SO.

我试图在 scala 中为匿名函数设置默认值,因此无法找到任何解决方案。希望有人能帮助我解决问题。

I have the following structure,

我有以下结构,

case class A(id:Int = 0)

case class B(a:A)

object B {
     def func1(f:Int = 0)={
      ........
     }
 def func2(f:A => B = (how to give default value ?))={
        case Nothing => {
         //do something....
        }
        case _ => {
         //do some other thing......
        }
 }
} 

Basically, I want to make passing the parameter as optional. How can I achieve this?

基本上,我想将参数作为可选参数传递。我怎样才能做到这一点?

回答by 4lex1v

Like any other default parameter:

像任何其他默认参数一样:

scala> def test(f: Int => Int = _ + 1) = f
test: (f: Int => Int)Int => Int

scala> test()(1)
res3: Int = 2

or with String:

或使用字符串:

scala> def test(f: String => String = identity) = f
test: (f: String => String)String => String

scala> test()
res1: String => String = <function1>

scala> test()("Hello")
res2: String = Hello

Edit:

编辑:

In case if you want to use a function provided by default, you have to use ()explicitly, either Scala won't paste a default argument.

如果您想使用默认提供的函数,则必须()明确使用,否则 Scala 不会粘贴默认参数。

If you don't wanna use a default function and provide an explicit one, just provide it yourself:

如果您不想使用默认函数并提供明确的函数,只需自己提供:

scala> test(_.toUpperCase)("Hello")
res2: String = HELLO

回答by itsbruce

Use an implicit parameter. Place an implicit value for the parameter in the object. This will be used unless you provide an explicit parameter or you have provided another implicit value in the calling scope.

使用隐式参数。为对象中的参数放置一个隐式值。除非您提供显式参数或在调用范围内提供了另一个隐式值,否则将使用此参数。

case class A(id:Int = 0)

case class B(a:A)

object B {
  implicit val defFunc: A => B = {a: A =>  new B(a) }
  def func1(f:Int = 0)={
  }
  def func2(implicit func: A => B) = { ... }
} 

The differences between this method and Alexlv's method are

这种方法和Alexlv的方法的区别在于

  1. This works with standalone functions as well as methods.
  2. The scope rules allow for providing appropriate overrides in appropriate scopes. Alex's method would require subclassing or eta-expansion (with partial application) to change the default.
  1. 这适用于独立的函数和方法。
  2. 范围规则允许在适当的范围内提供适当的覆盖。Alex 的方法需要子类化或 eta 扩展(部分应用)来更改默认值。

I offer this solution since you are already using an object. Otherwise, Alexvlv's example is simpler.

我提供此解决方案是因为您已经在使用对象。否则,Alexvlv 的例子更简单。

回答by Jethro

The other answers show how to provide some existing default value, but if you want the default to do nothing (as suggested by case Nothing) then you can use Option/None.

其他答案显示了如何提供一些现有的默认值,但是如果您希望默认值不执行任何操作(如 建议的那样case Nothing),那么您可以使用 Option/None。

 def func2(f:Option[A => B] = None)={
    case Some(f) =>
      //do something....
    case None =>
      //do some other thing......
 }


 func2()
 func2( Some(_ + 1) )