[Haskell-cafe] Over general types are too easy to make.

John Wiegley johnw at newartisans.com
Fri Aug 31 20:47:22 CEST 2012


>>>>>   <timothyhobbs at seznam.cz> writes:

>> data BadFoo =
>> BadBar{
>> badFoo::Int} |
>> BadFrog{
>> badFrog::String,
>> badChicken::Int}

> This is fine, until we want to write a function that acts on Frogs but not
> on Bars.  The best we can do is throw a runtime error when passed a Bar and
> not a Foo:

You can use wrapper types to solve this:

    data BadBarType  = BadBarType BadFoo
    data BadFrogType = BadFrogType BadFoo

Now you can have:

    deBadFrog :: BadFrogType -> String

And call it as:

    deBadFrog $ BadFrogType (BadFrog { badFrog = "Hey", badChicken = 1})

Needless to say, you will have to create helper functions for creating Bars
and Frogs, and not allow your BadBar or BadFrog value constructors to be
visible outside your module.

John



More information about the Haskell-Cafe mailing list