我正在尝试使用operator >
作为默认函数参数:
Playground execution failed: error: StackSorting.playground:27:63:
error: expected expression after unary operator
func sort<T>(..., compare: (T, T) -> Bool = >) where T: Comparable { }
^
我解决了,但是......有人知道更短的方式吗?
func sort<T>(..., compare: (T, T) -> Bool = { $0 > $1 }) where T: Comparable { }
答案 0 :(得分:4)
您可以使用运算符作为参数的默认值, 你只需将它括在括号中:
func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }