[Haskell-beginners] Type unions

Russ Abbott russ.abbott at gmail.com
Wed Dec 15 00:29:27 CET 2010


Thanks for the references.  Glad to see this isn't an unreasonable wish.
*
-- Russ *



On Tue, Dec 14, 2010 at 2:15 PM, Ozgur Akgun <ozgurakgun at gmail.com> wrote:

> This reminds me of an old thread started by, well me :)
>
> http://www.haskell.org/pipermail/haskell-cafe/2010-March/074805.html(sorry for the typos)
>
> It is not an especially enlightening thread, but contains some nice
> references.
>
> HTH,
>
> On 14 December 2010 20:09, Russ Abbott <russ.abbott at gmail.com> wrote:
>
>> Is there a way to get this to work?
>>
>>  data A = Aconstructor Int
>> data B = Bconstructor Int
>> data AorB = A | B
>>
>> f :: Int -> AorB
>> f x
>>   | even x     = Aconstructor x
>>   | otherwise = Bconstructor x
>>
>>  I get this diagnostic.
>>
>> Couldn't match expected type `AorB' against inferred type `A'
>>
>>
>> Since AorB is A or B, why is this not permitted?
>>
>> If instead I write
>>
>> data AorB = Aconstructor Int | Bconstructor Int
>>
>>
>> everything works out ok. But what if I want separate types for A and B?
>>
>> Thanks,
>> *
>> -- Russ *
>>
>>
> --
> Ozgur Akgun
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20101214/2594e6d2/attachment.htm>


More information about the Beginners mailing list