Enums
are a beautiful way of structuring information in Swift. Sometimes you
find yourself initializing enums from raw values, maybe because the
values were intermittendly stored somewhere else, say in the
NSUserDefaults
:
let aDevice = Device
print
Prints Optional(main.Device.phone)
The Problem
As soon as you're using associated values in your enums, this doesn't work anymore:
Swift can't create an instance of Example
because the two cases,
Factory and House have different associated types (the workers
integer and the street
string. Each invocation of Example
requires
different parameters, so this can't be generalized.
However, that's not the case when your associated types all match up:
In thise case, all the associated types
are the same. There're a
myriad of other ways to model this, but I found the device enum
to be
a concise example for what I'm about to explain. Even though every
Device
invocation is the same now, you still can't just call it with
some sort of raw value and expect the correct type. Instead, what you
have to do is perform a
match
in order to create the correct instance:
import Foundation
let b = Device.fromDefaults
print
prints Optional
This looks ok, but it is already a bit of repetitive code. Once you develop more than just three enum cases / two associated types, this will quickly get out of hand.
I think you get my point.
The Solution
So.. how do we solve this? Interestingly, there's a quirky similarity between the initializer of an associated type and a closure. Take this code:
let exampleClosure = Example.test
What is the type of exampleClosure
here? The type is
(Int) -> Example
. That's right, calling an associated value enum
case without any parameters will yield a closure that, when called with
the correct types, will return an instance of said type.
This means that, the following is valid, working Swift:
let appleMaker = Fruit.apple
let firstApple = appleMaker
let secondApple = appleMaker
print
So, how would that help us simplify the gross code duplication problem above? Have a look:
import Foundation
let iPhone = Device.fromDefaults
print
Optional
So, let's try to figure out what happened here. We have a new property
initializers
on our Device
. It's a Dictionary
of type
[String: (name: String, screenSize: CGSize) -> Device]
. I.e. something
that maps from a String
key to a closure with the same type as our
Device
cases. The dictionary contains the initializers of each of our
distinct cases, simply by using the same trick as above, just handing in
the closure: phone:Device.phone
The fromDefaults
function, then, only has to know the key of the
device we'd like to create, and it can call the appropriate closure.
This leads to a much shorter implementation, especially for bigger enums
(like our Vehicle example above). As you can see, creating a
Device
instance is then as simple as:
Device.initializers? )
Just as with raw values, in case there is no enum
case phone we'd
just get an empty optional back.
This solution isn't perfect of course. You still have to have the
initializers
dictionary, however it will be much less repetitve than
having to match
over all cases manually.
Finally, I suppose it goes without saying that the code above ignores an
important best practice to be concise and to be able to concentrate on
the task at hand; Nevertheless: having stringified code like
Device.initializers["phone"]
is not the best way to write this.
Instead, those keys should be properly defined somewhere else.