Opened 3 years ago

Closed 17 months ago

#5677 closed feature request (wontfix)

Allow the definition of extra functions in typeclass instances

Reported by: FUZxxl Owned by:
Priority: normal Milestone:
Component: Compiler Version: 7.2.1
Keywords: instance typeclass extension Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC rejects valid program Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets:

Description

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.

Change History (3)

comment:1 Changed 3 years ago by simonpj

Your suggestion makes perfect sense, but for me it falls just on the side of not-quite-worth-it. Let's see if there's a lot of other support.

Simon

comment:2 Changed 3 years ago by igloo

  • Difficulty set to Unknown
  • Milestone set to _|_
  • Status changed from new to infoneeded

comment:3 Changed 17 months ago by igloo

  • Resolution set to wontfix
  • Status changed from infoneeded to closed

No other support, so closing.

Note: See TracTickets for help on using tickets.