我试图在scala中为匿名函数设置默认值,因此无法找到任何解决方案.希望有人能帮助我.
我有以下结构,
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......
}
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我想将参数作为可选参数传递.我怎样才能做到这一点?
4le*_*x1v 16
像任何其他默认参数一样:
scala> def test(f: Int => Int = _ + 1) = f
test: (f: Int => Int)Int => Int
scala> test()(1)
res3: Int = 2
Run Code Online (Sandbox Code Playgroud)
或者使用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
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您想使用默认提供的函数,则必须()显式使用,Scala不会粘贴默认参数.
如果您不想使用默认功能并提供明确功能,请自行提供:
scala> test(_.toUpperCase)("Hello")
res2: String = HELLO
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9865 次 |
| 最近记录: |