Ticket #5677 (closed feature request: wontfix)
Allow the definition of extra functions in typeclass instances
|Reported by:||FUZxxl||Owned by:|
|Keywords:||instance typeclass extension||Cc:|
|Type of failure:||GHC rejects valid program||Difficulty:||Unknown|
|Test Case:||Blocked By:|
It would be nice, if one could define functions in an instance declaration that don't exist in the corresponding typeclass. Such a function should be only visible to the functions in the instance it is defined in.
I can imagine two purposes for such functions:
Consider a library MyLib? that has a typeclass Foo. In version 1.0 of the library, Foo is defined as:
class Foo a where foo :: ... bar :: ...
but in version 2.0, an additional member baz was added. If you want to write an instance of Foo that works both on 1.0 and 2.0, you currently have to use the preprocessor to hide the definition of baz if linked against library version 1.0. It would make writing code that works with multiple versions of other libraries much easier, if an extranous function in an instance is allowed.
The other purpose is that one can put common code of member functions in an extra function without moving the code out of the instance body. Together with ScopedTypeVariables?, this might turn out to be pretty useful.
As an addition, I would recommend to add a warning for the case, that an instance declares a function that is neither present in the class declaration nor used in the instance itself. This would catch most simple errors.