4 ms·
>in Haskell, by default you can't compare an Optional with the value it wraps: `5 == Just 5` fails. -- Probably nothing like in Swift instance (Num a ,
by GregBuchholz 10y ago
>in Haskell, by default you can't compare an Optional with the value it wraps: `5 == Just 5` fails.
-- Probably nothing like in Swift
instance (Num a , Integral a) => Num (Maybe a) where
fromInteger x = Just (fromIntegral x)
main = print $ 5 == (Just 5)
- majewsky 10y agoThis only works because you're working with number literals here, which have the very liberal type "Num a => a". If I replace your main function by check :: Int -> Bool check x = x == (Just x) main = print (check 5) I get a compile error: Main.hs:7:17: error: • Couldn't match expected type ‘Int’ with actual type ‘Maybe Int’ • In the second argument of ‘(==)’, namely ‘(Just x)’ In the expression: x == (Just x) In an equation for ‘check’: check x = x == (Just x)
- GregBuchholz 10y agoRight, it was just a fun little "Well Actually" moment. http://tirania.org/blog/archive/2011/Feb-17.html http://tirania.org/blog/archive/2011/Feb-17.html
- wyager 10y agoThat is possibly the most condescending thing I've ever read. Whatever the author tried to make themselves less abrasive, it didn't work very well.