I have recently been digging into the API of Apple CryptoKit and were wondering about one design decision in particular: Why ist it that many of the types are modelled as enum
s instead of struct
s even if there are no case
s defined (cf. eg. P256 and its nested types)? What are the benefits of this approach?
A case-less enum is Swift's closest approximation to a namespace.
The fact that case-less makes it impossible to accidentally instantiate.
6 Likes
Thanks, I suspected as much but was unsure if I was missing anything.
A struct, even one without properties, will receive a synthesized initializer, whereas an enum will not. This is why enums are used for namespaces.
1 Like