作为后续:scala贷款模式,可选功能参数
将withLoaner param移动到重载的apply方法的正确语法是什么?我试过以下几个版本失败了.此外,任何洞察我的错误在概念上非常赞赏.
def withLoaner = new {
def apply(n:Int, op: Int => String):String = (1 to n).map(op).mkString("\n")
def apply(n:Int, op: () => String):String = apply{n, i:Int => op()} // no compile
def apply(op: () => String):String = apply{1, i:Int => op()} // no compile
}
Run Code Online (Sandbox Code Playgroud)
Dan*_*ral 12
传递多个参数时,必须在它们周围使用括号.使用{}仅适用于单个参数.
此外,在使用函数文字时,如果指定类型,则必须将所有函数参数放在括号内.
所以,
def withLoaner = new {
def apply(n:Int, op: Int => String):String = (1 to n).map(op).mkString("\n")
def apply(n:Int, op: () => String):String = apply(n, i => op()) // no type on i
def apply(op: () => String):String = apply(1, (i: Int) => op()) // parenthesis around parameters
}
Run Code Online (Sandbox Code Playgroud)