Is there an equivalent to optional chaining with arithmetic operators?

zneak

I have two optional numbers that I add. Right now it looks like this:

if let a = optionalA {
    if let b = optionalB {
        return a + b
    }
}
return nil

For methods, there's the more convenient optional chaining syntax, like optionalA.?method syntax. Is there an equivalent for arithmetic operators that would return nil if either side was nil?

zneak

It's possible to create an operator +? that does just this like this:

infix func +?(a: Int?, b: Int?) -> Int? {
    if aa = a {
        if bb = b {
            return aa + bb
        }
    }
    return nil
}

but I'm wondering if there's another, built-in way to do that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related