Exhaustive parsing of enums with associated values #196
Replies: 1 comment
-
Hey @lukeredpath! Thanks for the suggestion! I think functionally, If so, that definitely sounds like a nice custom parser, but it's a surprisingly nuanced problem. With I'm actually not even sure this is technically possible, but if I could take some of our learnings and help sketch out some of the known requirements:
This is just the surface of what sticks out to me, if you choose to go down the path of discovery. Unfortunately I'm just not certain it's possible 😅 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The library makes it fairly easy to parse some input into cases of an enum with associated values, albeit with a bit more work than raw representable enums. For example, given this structure:
We can parse input in the form "type:value" e.g.
"int:123"
intoWrappedValue.int(123)
using the following parser:One thing that would be really nice would be to have some kind of dedicated enum case parser - effectively a special case of
OneOf
that enables exhaustive checking, i.e. that you have handled every case ofWrappedValue
in the parser. This would have to be a runtime warning rather than a compile-time check but I still think this would be useful.My inspiration here is the
SwitchStore
in TCA that warns you if you do not exhaustively handle each case and this could probably borrow implementation ideas from there too. Like SwitchStore, it would require some generated variadics to handle different numbers of cases.Hypothetical syntax:
Beta Was this translation helpful? Give feedback.
All reactions