我有以下方法:
scala> def method_with_default(x: String = "default") = {x + "!"}
method_with_default: (x: String)java.lang.String
scala> method_with_default()
res5: java.lang.String = default!
scala> method_with_default("value")
res6: java.lang.String = value!
我正在尝试使用 val 来实现相同的效果,但出现语法错误,如下所示:
(没有默认值,这个可以编译)
scala> val function_with_default = (x: String) => {x + "!"}
function_with_default: String => java.lang.String = <function1>
(但我无法编译这个...)
scala> val function_with_default = (x: String = "default") => {x + "!"}
<console>:1: error: ')' expected but '=' found.
val function_with_default = (x: String = "default") => {x + "!"}
^
任何的想法?