Reputation: 1324
Could someone explain to me what this code snippet does (mainly last two lines)? Shouldn't I be able to pass Nothing to defValue? I tried and got an error "No instance for (Def, a0) arising from a use of 'defValue'"
class Def a where
defValue :: a -> a
instance Def a => Def (Maybe a) where
defValue ~(Just x) = Just (defValue x)
Upvotes: 1
Views: 139
Reputation: 120731
This problem has nothing to do with patterns, in fact you would have got the same error message if you'd declared
instance Def a => Def (Maybe a) where
defValue = undefined
In fact, your code should work (as far as it possibly can, however I'd say your entire idea isn't good, very unsafe without need), but to use that Maybe
instance you need an instance of the packed type to work with. For instance, if you add
instance Def Int where
defValue _ = 0
then you can do
DefVals> defValue (Just 1 :: Maybe Int)
Just 0
DefVals> defValue (Nothing :: Maybe Int)
Just 0
Upvotes: 3