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

Art*_*tch 5 closures default function operators swift

我正在尝试使用运算符>作为默认函数参数:

Playground execution failed: error: StackSorting.playground:27:63: 
error: expected expression after unary operator
func sort<T>(..., compare: (T, T) -> Bool = >) where T: Comparable { }
                                            ^
Run Code Online (Sandbox Code Playgroud)

我解决了它,但是......有人知道更短的方法吗?

func sort<T>(..., compare: (T, T) -> Bool = { $0 > $1 }) where T: Comparable { }
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 6

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

func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }
Run Code Online (Sandbox Code Playgroud)