Non-frozen enums in Swift

As I understand it, as a user of Swift, I can't define a non-frozen enum in Swift?

Presumably there's a reason for that? It would seem beneficial if we could as we could then avoid breaking changes in library APIs when adding new cases to the enum.

Currently a user of my library gets frozen enums which I may intend to add new values to in the future. They have to write exhaustive switch statements, which might take the form a listing our each possible case, with no default case, so adding a new value will break compilation.

In ObjC, or Apple's code, enums can be defined as non-frozen to avoid this issue.

1 Like

You’d need to use library evolution support.

See some background here:

(There are several caveats though - one of the biggest ones that this is not yet fully supported cross platform yet)

Ahh nice, my sample library/app I was playing around with this in didn't have that enabled of course, but the production library should be able to

You might also want to check out eg.

And

For at least two of the current caveats.

Thanks sharing! SPM and/or Linux aren't currently a concern and our current approach isn't affected by the second one I don't think (but either might apply to others reading of course!).

If you want to do proper library evolution tied to versions of your library, I think the second one would be something that would impact.

3 Likes

That’d be much more lightweight for some use cases, thanks for the pointer.