我可以在 Swift 中使用运算符作为默认函数参数吗?
Can I use an operator as default function argument in 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 { }
^
我解决了,但是...有人知道更短的方法吗?
func sort<T>(..., compare: (T, T) -> Bool = { [=12=] > }) where T: Comparable { }
您可以使用运算符作为参数的默认值,
您只需将其括在括号中:
func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }
我正在尝试使用运算符 >
作为默认函数参数:
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 = { [=12=] > }) where T: Comparable { }
您可以使用运算符作为参数的默认值, 您只需将其括在括号中:
func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }