pattern-matching with labelled types

Andre W B Furtado awfurtado@uol.com.br
Thu, 7 Mar 2002 22:49:00 -0300


If I have:

> data MyType = MT {
>     x :: Int,
>     y :: Char
>     }

How do I update the Int value of MyType leaving the Char value unaffected? I
tryied something like:

> MT {x = newValue}

but GHC gave me a warning about the Char value and it indeed caused strange
effects. Of course, it is possible to do something like

> update :: MyType -> Int -> MyType
> update mt newValue = MT {x = newValue, y = oldValue}
>     where oldValue = y mt

but this really annoys me when MyType has too many fields. Suggestions?

Thanks a lot,
-- Andre