我可以在Swift中使用运算符作为默认函数参数吗?

时间:2017-08-14 06:16:31

标签: swift function closures operators default

我正在尝试使用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 { }

1 个答案:

答案 0 :(得分:4)

您可以使用运算符作为参数的默认值, 你只需将它括在括号中:

func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }
相关问题