One thing that comes to mind - and maybe it’s solved trivially - but what happens for the cases where, both enum are marked as Int
and, therefore, their cases correspond to 0…n
- what would happen if we joining two distinct enum
each with their own with separate ordering?
Ex
enum MyFirstEnum: Int { case a, case b}
enum MySecondEnum: Int { case x, case y, case z}
Now let’s say I’m doing something with an XPC
connection, let’s say I have a launch agent written entirely in objective c which is forwarding an object over to swift, and, I know on the other side of my connection I have some swift code which I plan to use the contents of the object to represent an enum
case - how would I be able to hit the right case if I had some swift code receiving the object? In such a scenario, maybe it’s solved simply - I’m not sure.. Maybe I would have to handle the cases on the components of the composition before I start making use of the composed enum?
Ex from some objc code sending an object to swift where it knows the swift code is translating the contents to enum cases
NSDictionary *blah = @{@“myKey”: 0}; // where I believe 0 corresponds to `MyFirstEnum case a`
[self->delegate sendObj(object: blah)];
On the swift side let’s say I receive that dictionary - where would I put it in the composed Enum
on the swift side? Is “0” MyFirstEnum.a
or MySecondEnum.x
If someone was trying to represent the raw value of the hypothetical joined set, what would be the Int value for each case? Is it based on the order they were built?
class: SwiftSide: DelegateProtocol {
func sendObj(object: [String: Any]) {
var myComposedEnum: MyComposedEnum?
myComposedEnum = MyComposedEnum.init(rawValue: object[“myKey”]) // which case belongs to who?
}
}
What would be the best way to tackle such a scenario? Is 0
corresponding to case a
of MyFirstEnum
or perhaps corresponding to case x
of MySecondEnum
? I think that it’s something that needs an concrete answer because MyComposedEnum
is representing case a, b, x, y, z
Maybe it’s a non-issue or it’s something that can be solved elegantly
In any case I like the idea and I can see how it would be useful to be able to represent multiple enums through one. I tend to avoid enum if possible but they are quite useful nonetheless.
It does seem fragile for those situations where (and I know this is an arcane corner case) someone has something like an XPC connection, it’s taking in some runtime selectors, it’s passing an object that is supposed to map over to a swift enum
. I think if someone comes in on swift side and plops down a conformance that essentially becomes “MyComposedEnum”, the cases preservation is going to be challenging
Perhaps if MySecondEnum adopts the conformance and points to MyFirstEnum, the cases are concatenated so it becomes like 0…n starting from 0 with MyFirstEnum ?