Say I have a simple class AClass with a public member f1 which can be overridden. What are the ways to define a new instance of AClass with another member f2, short of duplicating the source code of AClass? Toy code below:
class AClass a where
f1 :: a -> Int
data Val = I Int
instance AClass Val where
f1 x = 0
-- the method below can't be added as it is not public member of AClass
-- f2:: a -> Float
-- f2 x = 0.0
I looked around but I didn’t find any clear examples on how to do it (i.e., examples I could understand well – clarity is relative). What are the possible ways? Closure, newtype declaration or something else? It will be helpful to demonstrate the technique with above toy code – you can change data declaration etc. (e.g., replace it with a newtype wrapper around Int) but the only thing immutable in above code is class declaration of AClass. This is because the assumption is the class has already been written by a library writer, and so, I can’t touch it. The end result should be another toy code which inherits the goodies of AClass, and adds f2 member.
There will be caveats of course, in overriding classes like this. But, it helps to know what is possible, and how.
— Update —
Working code below – Credit to Ben and mergeconflict for coming up with the solution – there were few missing pieces – filled in below:
class AClass a where
f1 :: a -> Int
class (AClass a) => BClass a where
f2 :: a -> Float
data Val = I Int
instance AClass Val where
f1 _ = 0
instance BClass Val where
f2 _ = 0.0
What are you trying to achieve?
You have a type
Val, which you make an instance ofAClass. You can define any number of functions that useValthat have nothing to do with the class. Just stop trying to define them in theinstancedeclaration.If what you’re expecting is to be able to have one particular instance of
AClassthat has an extraf2function, which you then use in functions that useAClassinstances and have them be able to callf2… that’s absurd. By definition, the only things that are known to be common to allAClassinstances are the things declared inAClass. If all you know about some value is that it is a member of a type that is an instance ofAClass, you can’t do anything with it that you can’t do with all instances ofAClass. You can’t call anything extra that’s particular to certain instances.If you want to create a new class that supports all of the operations that
AClassdoes as well asf2, and haveValbe an instance of that new class… then you just do that.