简体   繁体   English

如果在Swift中使用运算符,则返回Infix

[英]Infix return if operator in Swift

I have much of validation like this 我有很多这样的验证

if (someBoolean) return

Can I create an infix operator with following syntax? 我可以使用以下语法创建中缀运算符吗?

returnif someBoolean

No, you can't write this operator (which would be prefix rather than infix), for two reasons: 不,您不能编写此运算符(该运算符为前缀而不是中缀),原因有两个:

  • operators can't be defined with alphanumeric characters (see the docs for a rundown of what they can be defined with) 运算符不能用字母数字字符定义(有关可定义的内容的摘要,请参见文档
  • you can't write a function (which is what custom operators are) that results in a return in it's calling scope ie return within the operator function would just return from the operator function, not from the function you used the operator in 您无法编写一个函数(这是自定义运算符),从而导致其调用范围内的返回,即,运算符函数内的返回只会从运算符函数中返回,而不是从您在运算符中使用过的函数中返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM