Skip to content

Conditional parsing based on previous output #288

Answered by tgrapperon
andrews05 asked this question in Q&A
Discussion options

You must be logged in to vote

Hey @andrews05. There are indeed several ways to implement a solution to your problem, but the simplest is probably to attack it at its root: OpWithValue is a type that allows incoherent states (that is opA with a nil value for example). It means that you need to take of this at the parser level and prevent it to create such values by failing if the input is not coherent. You can for example use filter to discard invalid configurations.

But I feel that it would be preferable to model your domain so it prevents incoherent states. One way to do it is to make the code in the enum:

enum Op {
  case opA(Int)
  case opB(Int)
  case opC(Int?)
}

You can then create a few helpers:

extension Op {
  s…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@tgrapperon
Comment options

@andrews05
Comment options

@tgrapperon
Comment options

@tgrapperon
Comment options

@andrews05
Comment options

Answer selected by andrews05
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants