(Note that the title should actually be: Operator to avoid "Left side of nil coalescing operator '??' has non-optional type" but one ? disappears when saving the topic.)
You could cast the non-optional back into the optional type. For example, for the warning here:
func foo(x: Int) -> Int? {
return x ?? 0 // ⚠️ Left side of nil coalescing operator '??' has non-optional type 'Int', so the right side is never used
}
Can be worked around by casting the non-optional to an optional:
func foo(x: Int) -> Int? {
let x = x as Int?
return x ?? 0 // ✅
}
Ideally you'd have access to the unwrapped type in your macro, but the workaround can be made to work even without that:
func foo(x: Int) -> Int? {
let x = x as Optional<_>
return x ?? 0 // ✅
}
I can't think of any example outside of macros where the compiler can prove statically that the value is non-optional in which I'd also want to provide a default value.
This warnings problem with macros isn't unique to optionals either. I've been facing a very similar thing this week with a macro that expected a function to be async, but could also be applied to synchronous functions. So I got a "No 'async' operations occur within 'await' expression" warning in those expansions, and I had to cast the function to an async type before using it.
What I'd want in the language is a way to silence certain kind of warnings in macros as 'expected', instead of having to come up with convoluted ways to suppress the warning.