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

Luke Palmer lrpalmer at gmail.com
Wed Jul 29 13:58:44 EDT 2009


On Wed, Jul 29, 2009 at 6:27 AM, Paul Sujkov<psujkov at gmail.com> wrote:
> 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

I am not sure what you're asking.  Are you saying that what you have
written is boilerplate?  What code are you writing that could be
automatically deduced with enough smartness?

Give an example of what you might like the solution to look like.

Luke

> 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
> advance
>
> --
> Regards, Paul Sujkov
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


More information about the Haskell-Cafe mailing list