Extending Optional
Extend Optional so that any optional can be asked for its nil status, like someOptional.isNil. How would you implement it?
Extend Optional so that any optional can be asked for its nil status, like someOptional.isNil. How would you implement it?
The standard Optional is an enum with none and some cases, so add an isNil computed property in extension Optional that returns true when self matches .none and false otherwise. Since it's pattern matching, it works on any optional with no Equatable or other constraint on Wrapped.
SwiftMultiple choiceBasics
Which statements about Swift enum are correct? Pick all that apply.
#enum #pattern-matching
SwiftShort answerIntro
Why does Swift have optionals?
#optional #enum
SwiftShort answerBasics
Extend Array with a myCompactMap that behaves like the standard library's compactMap. How…
#optional #extension
SwiftShort answerAdvanced
Suppose there were no question-mark optional syntax and no standard Optional. If you had…
#optional #enum