【发布时间】:2016-11-09 09:36:03
【问题描述】:
乘法符号的定义是这样的。
public func *(lhs: Int, rhs: Int) -> Int
我可以在函数中使用它,就像这样。
func productUsingReduce(xs: [Int]) -> Int {
return xs.reduce(1, { x,y in x * y})
}
或者就这样。
func productUsingReduce(xs: [Int]) -> Int {
return xs.reduce(1, *)
}
如果我尝试用不同的名称定义相同的事物。
func yes(lhs: Int, rhs: Int) -> Int {
return lhs * rhs
}
当我尝试像这样使用它时出现编译器错误。
func productUsingReduce(xs: [Int]) -> Int {
return xs.reduce(1, { x,y in x yes y})
}
为什么?
为什么下面的 sytnax 不能编译?
func productUsingReduce(xs: [Int]) -> Int {
return xs.reduce(1, { x, y in *(lhs: x, rhs: y) })
}
【问题讨论】: