Daily iOS
Swift

Designing a custom Optional

Suppose there were no question-mark optional syntax and no standard Optional. If you had to design a type in Swift that expresses 'a value or nothing,' how would you define it?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

Define a generic enum Maybe<Wrapped> with two cases, .none for no value and .some(Wrapped) for a value, and extract with switch pattern matching. The standard Optional has exactly this structure.

Share a question