In my code, I check if a system function is equal to zero or not, if yes I return an other value, if not, I return the tested value.
(class.verylongfunc(arg, arg) == 0) ? othervar : class.verylongfunc(arg, arg)
Here is a better and cleaner way to do that ? I don't want to call the function two times... I know I can do
let tmp = class.verylongfunc(arg, arg)
(tmp == 0) ? othervar : tmp
But it take two lines...
Do you control verylongfunc
? If so, is 0 a special case meaning some kind of failure or no possible return? If so, have it return nil, then you can do
class.verylongfunc(arg, arg) ?? othervar
If not, make your own operator:
infix operator ??? {}
func ???(num: Int32, defNum: Int32) -> Int32 {
if num == 0 {
return defNum
}
return num
}