[Haskell-cafe] [Haskell Cafe] Data construction: how to avoid boilerplate code?

Paul Sujkov psujkov at gmail.com
Wed Jul 29 10:27:07 EDT 2009

Hi haskellers,

I have a datatype of this sort:

data Type = Status
          | Message
          | Warning
          | Error
          | StepIn
          | StepOut deriving (Eq, Show)

and (at this moment) two fabric-like functions:

makeType :: String -> Type
makeType c = case c of
               "-$-"       -> Status
               "-M-"       -> Message
               "-W-"       -> Warning
               "-E-"       -> Error
               "->>"       -> StepIn
               "<<-"       -> StepOut
               otherwise   -> error "Uknown Message Type"

deduceType :: Integer -> Type
deduceType n = case n of
                 240       -> Status
                 64        -> Message
                 32        -> Warning
                 8         -> StepOut
                 4         -> StepIn
                 2         -> Error
                 otherwise -> error "Unknown Message Type"

how can I avoid boilerplate code at this stage? The thing that I really need
is some n-type constructor, kind of a fabric for a variety of types with the
possibility to add them on the fly (I have simple Integer and String here,
but they could be much more sophisticated). I don't need the possibility to
unpack the original value (e.g. "-$-" or 240) once the Type is created - in
this case I can always have some sort of mapping to deduce it at any moment,
so it will be redundant to carry it through the code explicitly

The example is rather short and simple, but I have some more places in code,
where the same problem is observed. Is there any generic solution? Thanks in

Regards, Paul Sujkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090729/0b2a7ede/attachment.html

More information about the Haskell-Cafe mailing list