Re-implement optionals in Swift
2 min readMar 30, 2019
In Swift optionals are implemented as enums that contain some or none.
Prerequisites:
- Some basic understanding of Enum and associated values
Terminology
Optionals: Swift introduced optionals that handle the absence of a value, simply by declaring if there is a value or not. An optional is a type on it’s own!
enum: An enumerated type is a data type consisting a set of members of the type.
The official version of Integer optionals
The idea here is that we will try to implement optionals much how it is performed in Swift.
So in Swift we can create optional Ints with the following:
Implementing Integer optionals in Swift
So optionals can be implemented through an enum (with associated value).