My colleague Michael Ilseman shared a design problem with me today, which came down to something like this:
Today, we have a protocol RawRepresentable, which says that “if I have a type X
, I can convert it to and from X.RawValue
”. You can then make a whole host of types that use Int
as the raw value, and indeed that’s how Swift represents enums imported from Objective-C. (Well, those with an NSInteger
underlying type, at least.) That makes this, in some sense, a “many-to-one” relation, because many types can have the same one raw value type. But what you can’t do is make a single type have multiple RawValue
types—one of them has to claim the name X.RawValue
.