我正在尝试声明一个自定义后缀运算符来计算给定数字的阶乘,有没有办法使这个函数递归(数字-1)!!?
postfix operator !! {}
postfix func !! (number: Double) -> Double {
if (number <= 1) { return 1.0 }
return number * (number-1)!!
}
最佳答案
我认为这是不可能的,因为它与用于展开可选值的 !
冲突。
来自 The Swift Programming Language: Keywords and Puncutation
The following tokens are reserved as punctuation and can’t be used as custom operators:
(
,)
,{
,}
,[
,]
,.
,,
,:
,;
,=
,@
,#
,&
(as a prefix operator),->
,`
,?
, and!
(as a postfix operator).
但是,在我看来,如果是这种情况,那么 postfix operator !! {}
有效。我推荐 filing a bug .
例如,您可以使用 ‼
(双感叹号;U+203C)来解决这个问题。
关于ios - swift 中的后缀递归自定义运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32289506/