I think if case
fits well once you get used to it. I have several complaints, however:
- When you type the
if case let
statement, the sourcekit is not able to make an autocomplete suggestion, as it does withswitch case
, because the variable you want to match is typed last. One solution from my point of view, would be to allow the matched variable on left side, like this:
enum A { case a(Int), b(Int) }
let a = A.a(1); let b = A.b(2)
if a = case let .a(anInt), case let .b(anInt2) = b { print(anInt + anInt2) }
// or
if case a = let .a(anInt), case let .b(anInt2) = b { print(anInt + anInt2) }
// or even requirement to use pattern mathing operator would be fine fmpov
if a ~= let .a(anInt), case let .b(anInt2) = b { print(anInt + anInt2) }
- The other factor that makes me less enthusiastic about enums (again, fmpov) is, that In-place mutation of an enum associated value is still not possible. If this was possible, enums would get huge advantage over any "sealed" protocol based solution.