我可以在Swift中使用运算符作为默认函数参数吗? [英] Can I use an operator as default function argument in Swift?
本文介绍了我可以在Swift中使用运算符作为默认函数参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用运算符>
作为默认函数参数:
I'm trying to use operator >
as default function argument:
Playground execution failed: error: StackSorting.playground:27:63:
error: expected expression after unary operator
func sort<T>(..., compare: (T, T) -> Bool = >) where T: Comparable { }
^
我解决了,但是...有人知道一个简短的方法吗?
I solved it, but... Does somebody know a shorter way?
func sort<T>(..., compare: (T, T) -> Bool = { $0 > $1 }) where T: Comparable { }
推荐答案
您可以将运算符用作参数的默认值,您只需将其括在括号中即可:
You can use the operator as default value for the parameter, you only have to enclose it in parentheses:
func sort<T>(..., compare: (T, T) -> Bool = (>)) where T: Comparable { }
这篇关于我可以在Swift中使用运算符作为默认函数参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文