<div dir="ltr">Hi Yotam,<div><br></div><div>A type variable `a` means that you know absolutely nothing about the variable, and can't treat it specially. So you can't special-case your function to "either Foo or Bar."</div><div><br></div><div>You can achieve your goal like this:</div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">func :: Either Foo Bar -> (Int -> Int)</font></div><div><font face="monospace, monospace">func (Left (Foo a _)) = a<br>func (Right (Bar a)) = a</font><br></div><div><br></div><div>The kind of runtime type inspection you might be used to from other langauges is available via the Typeable class. It's extremely non-idiomatic Haskell, and I strongly recommend against writing this sort of code. For completeness, this also works:</div><div><br></div><div><font face="monospace, monospace">func :: Typeable a => a -> Maybe (Int -> Int)<br>func a = maybeFoo <|> maybeBar<br>Â where<br>Â Â maybeFoo = case cast a of<br>Â Â Â Just (Foo a _) -> Just a<br>Â Â Â Nothing -> Nothing<br>Â Â maybeBar = case cast a of<br>Â Â Â Just (Bar a) -> Just a<br>Â Â Â Nothing -> Nothing</font></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Matt Parsons</div></div></div></div>
<br><div class="gmail_quote">On Tue, Oct 10, 2017 at 10:56 AM, Yotam Ohad <span dir="ltr"><<a href="mailto:yotam2206@gmail.com" target="_blank">yotam2206@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="rtl"><div dir="ltr">Hello cafe,</div><div dir="ltr">I am trying to do the following:</div><div dir="ltr"><br></div><div dir="ltr"><font face="monospace">data Foo = Foo { a1 :: Int -> Int, a2 :: Int -> Char }</font></div><div dir="ltr"><font face="monospace">data Bar = Bar { a1 :: Int -> Int }</font></div><div dir="ltr"><font face="monospace"><br></font></div><div dir="ltr"><font face="monospace">func :: a -> Maybe (Int -> Int) -- a is either Foo or Bar</font></div><div dir="ltr"><font face="monospace">func (x::(Bar/Foo) = Just $ a1 x</font></div><div dir="ltr"><font face="monospace">func _ = Nothing</font></div><div dir="ltr"><font face="monospace"><br></font></div><div dir="ltr">I'm not sure how to implement this. All I know that the types are matching so I think it could be possible.</div><div dir="ltr"><br></div><div dir="ltr">Thanks for your help</div><span class="HOEnZb"><font color="#888888"><div dir="ltr"><font face="monospace">-</font>Yotam</div></font></span></div>
<br>______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>