[Haskell-beginners] Question: Data Type for user selection

Brandon Allbery allbery.b at gmail.com
Tue Aug 16 05:43:11 CEST 2011


On Mon, Aug 15, 2011 at 19:35, Hartmut <hartmut0407 at googlemail.com> wrote:

> data BasicSelect a =  NumberSelect a | StringSelect a
> data Num a => NumberSelect a = SelectionNum a
> data Show a => StringSelect a = SelectionStr a
>

This seems a bit confused.  Additionally, the contexts don't do what you
intend (they're nearly useless and will probably be removed in a future
Haskell revision).

I think what you're reaching for here is a GADT:

> data BasicSelect a where
>   SelectionNum :: Num a => a -> BasicSelect a
>   SelectionStr :: Show a => a -> BasicSelect a

I think you otherwise end up with a typeclass or with existentials; both
introduce complexity, and I think the existentials solution still allows
anything to be stuffed into it, whereas you want to limit it to SelectionNum
or SelectionStr.  (Typeclasses at least require an instance to be defined
first; but they don't prohibit people from doing so, if that's what you're
after.)

-- 
brandon s allbery                                      allbery.b at gmail.com
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110815/d6500b41/attachment.htm>


More information about the Beginners mailing list