Nested Enums: The What, Why and When

They aren’t too hard!

Steven Curtis
2 min readJul 7, 2021
If you’d prefer the video version — https://youtu.be/I6j-suUEOts

We can add enums within enums, or even within a Struct or a Class in order to make our software design come alive.

This article will take us through just that.

The example

During 2020 the UK experienced various lockdowns. During some of these the number of acceptable activities for people resident in the UK was limited.

Therefore I’ve created a example right here:

The issue

There isn’t a massive issue here. If you want to use these enum in various places within your module the default (internal) access control would not be an issue.

But what if you want (and you should), to do better?

We could make any particular enum have a private level of access control. This isn’t exactly wrong, but we can do better.

Nested enum

We see that final property, the run one:= let run = PermittedExercise.run.

--

--

Responses (1)