Allow the definition of extra functions in typeclass instances
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.
Trac metadata
Trac field | Value |
---|---|
Version | 7.2.1 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |