For example, I have a type class :
class MyClass a b c where
fun01 :: a -> b
fun02 :: a -> c
fun03 :: a -> b -> c -> ()
fun04 :: a -> WhatEver
I'd like to provide a default implementation for my, let's call it BaseDataType
which defines implementations of fun03
in terms of it self and fun01
and fun02
. Then I'd have something like this :
class MyClass BaseDataType b c where
fun03 = fun01 <$> fun02 ...
fun04 = fun02 ...
And than to finalize my class instance and avoid all the boilerplate code for fun03
and fun04
I'd just provide fun01
and fun02
like this :
instance MyClass BaseDataType Int Char where
fun01 = 1
fun02 = 'C'
Is there possibly some language extension that allows this kind of behaviour? I couldn't find anything on this topic.
There is no such extension, but you can achieve this functionality simply by splitting your class into two classes:
Then your instances will work the way you want:
Users of your class are not affected; they can continue to consume
MyClass2
where they usedMyClass
before and get the exact same functionality.Aside: the original definition of
MyClass
, andMyClass1
andMyClass2
don't even compile due to several ambiguous type errors (c
is not mentioned in the type offun01
, etc.) - I assume this class was defined just for demonstration purposes and I haven't tried to fix this.