Search code examples
haskellalgebraic-data-types

Is there a better way to express this type?


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?


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