[Haskell-cafe] Pattern Matching with Different Types

Adam Bergmark adam at bergmark.nl
Tue Oct 10 17:33:31 UTC 2017

Hi Yotam,

One way to do this is to take an `Either Foo Bar` as an argument instead,
then you can pattern match on its Left and Right. This is preferable if the
function is only meant to work for a limited number of types. If you want
it to be extensible to any number of types you can write a typeclass that
contains `func` with the same type signature as you have now. There are
also some extensions built in to GHC to do this automatically for you, but
I don't recall exactly which ones to enable...


On Tue, 10 Oct 2017 at 18:58 Yotam Ohad <yotam2206 at gmail.com> wrote:

> Hello cafe,
> I am trying to do the following:
> data Foo = Foo { a1 :: Int -> Int, a2 :: Int -> Char }
> data Bar = Bar { a1 :: Int -> Int }
> func :: a -> Maybe (Int -> Int) -- a is either Foo or Bar
> func (x::(Bar/Foo) = Just $ a1 x
> func _ = Nothing
> I'm not sure how to implement this. All I know that the types are matching
> so I think it could be possible.
> Thanks for your help
> -Yotam
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20171010/72cf43c1/attachment.html>

More information about the Haskell-Cafe mailing list