Question

I've made this data type, which has a lot of repetition in it.

data JobState = UnsanitizedData Handle
              | SanitizedData Handle
              | VerifiedData Handle
              | JobFail Handle

I don't want any of these JobStates to ever be used without Handle. But the repetition has me suspecting I'm not expressing this correctly. Is there a better way?

Was it helpful?

Solution

One way would be to factor this out into a state value, and a wrapper that includes the handle:

data JobState = UnsanitizedData | SanitizedData | VerifiedData | JobFail
  deriving (Eq, Enum, Ord, Show, Read)
data Job = Job { jobState :: JobState, jobHandle :: Handle }
-- or: data Job = Job JobState Handle
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top