<div dir="ltr">If you want a function's behavior to vary by the types at which it is used, you'd need to use a type class. For example:<div><br></div><div><div><font face="monospace">data Foo = Foo { a1 :: Int -> Int, a2 :: Int -> Char }</font></div><div><font face="monospace">data Bar = Bar { a3 :: Int -> Int }</font></div><div><font face="monospace"><br></font></div><div><font face="monospace">class    Func a   where func :: a -> Maybe (Int -> Int)</font></div><div><font face="monospace">instance Func Foo where func = Just . a1</font></div><div><font face="monospace">instance Func Bar where func = Just . a3</font></div></div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Oct 10, 2017 at 10:42 AM Matt <<a href="mailto:parsonsmatt@gmail.com">parsonsmatt@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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="m_-3791201421312063130gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Matt Parsons</div></div></div></div></div><div class="gmail_extra">
<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="m_-3791201421312063130HOEnZb"><font color="#888888"><div dir="ltr"><font face="monospace">-</font>Yotam</div></font></span></div>
<br>_______________________________________________<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-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.<br></blockquote></div><br></div>
_______________________________________________<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-bin/mailman/listinfo/haskell-cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div>