[swift-users] The value of enums
Rien
Rien at Balancingrock.nl
Sun Nov 6 06:51:10 CST 2016
As usual: it depends.
I use enums with associated values quite a lot.
In my experience there is quite a thin line between very convenient and too much.
I have several cases where I started off with enums with associated values, only to later convert them to classes.
As long as the enums only have an associated value, things tend to work just fine. But as soon as more properties become associated with the enum, things get out of hand.
Once I tried to fix a complete inter process protocol in enum’s. And it did work, but maintenance became a nightmare after the protocol implemented more than 10 commands or so.
To me the key is: keep them small.
Regards,
Rien
Site: http://balancingrock.nl
Blog: http://swiftrien.blogspot.com
Github: http://github.com/Swiftrien
Project: http://swiftfire.nl
> On 06 Nov 2016, at 12:07, Tino Heth via swift-users <swift-users at swift.org> wrote:
>
> Enums are a fundamental part of Swift, so I guess they won't change much — but I wonder if anyone shares my observations in real-life use…
>
> Afair, there are three different types of enums:
> - Enums with raw values
> - enums with associated objects
> - Plain enums (no underlying value)
>
> I use the first type quite often (as a convenient way to create string constants, or for serialization), but see no real value in plain enums (they offer nothing over enums backed with a raw value).
>
> The second type is special:
> It looks like a really cool concept, and and I started several designs based on them — just to realize later that structs and classes are a better fit.
> My conclusion so far is that enums perform bad as soon as you want to attach additional data or behavior; one or two computed properties are ok, but those switch-statements quickly become a burden.
> There are some options to work around this problem, but I guess I'll just stay away from enums with associated objects by default (with the exception of error-types — imho those can be modeled quite nicely).
>
> So, that's my current perception, and I'm curious if others had similar experiences — or, even more interesting, completely different observations and elegant solutions based on enums.
>
> - Tino
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
More information about the swift-users
mailing list