i have this
data Something = Something Integer deriving (MyClass, Show)
class MyClass a where
hello :: MyClass a => a -> a
instance MyClass Integer where
hello i = i + 1
main = print . hello $ Something 3
but MyClass isn’t derivable. Why?
GHC cannot magically derive instances for arbitrary data types. However, it
can make use of the fact that
newtypedeclarations create a new name for thesame underlying type to derive instances for those using the
GeneralizedNewtypeDerivingextension. So, you could do something like this:The reason GHC cannot derive the new instance is that it does not know what the instance
should be. Even if your data type only has one field, it may not necessarily be the
same as that field. The ability to derive instances for
newtypes is convenient, since theyare usually used to provide different behaviours for certain typeclasses or as a way to
use the type system to separate things that have the same type but different uses in your code.