Extracting values from several data constructors

Dean Herington heringto@cs.unc.edu
Wed, 23 Apr 2003 19:20:29 -0400 (EDT)


[redirected to haskell-cafe]

On 24 Apr 2003, Steffen Mazanek wrote:

> Hello.
> I am sure this question is not new. Even so, please
> give me the solution or point me to an old thread.
> 
> How can I implement something like:
> 
> data Test = T1 Int | T2 Int
> test::Test->Int
> test (_ x) = x
> 
> Or is this impossible? In case of yes, why?
> This would save a lot of case differentiations in 
> my application. Or is there an appropriate compiler option?

It can be done, but only for labeled fields.  For example:

    data Test = T1 { test :: Int } | T2 { test :: Int }

See section 3.15 of the Haskell report.

Dean