Extracting values from several data constructors

Hal Daume III hdaume@ISI.EDU
Wed, 23 Apr 2003 16:20:20 -0700 (PDT)


In general, this is not possible.  Perhaps if you were to supply a type
signature, I could understand what you wanted the compiler to do, but
without, it's just too hard.

What you can do is use record labels.  Here, you just name the two Ints
the same thing and get what you want:

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

this is more general, in that you can say:

> data Shape = Circle { center_x :: Double, center_y :: Double, radius
>                  :: Double }
>            | Ellipse { center_x :: Double, center_y :: Double, radius_x
                   :: Double, radius_y :: Double }

and get all the functions for free

--
 Hal Daume III                                   | hdaume@isi.edu
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume

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?
> 
> Good night,
> Steffen
> 
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>