Local definitions in the class instances

Nick Bowler nbowler at elliptictech.com
Thu Jan 27 19:02:39 CET 2011

On 2011-01-27 13:07 +0200, Boris Lykah wrote:
> I think it would be convenient to allow adding variables and
> functions, which are not members of the class,  to a class instance so
> that they are visible only in the instance scope. It will help if the
> same functions are used by several class functions.
> Example:
> When implementing Num class for my datatype, I found that I routinely
> do unwrapping in each operator definition. I extracted it into
> functions, but as they are used only in instance definition, I want to
> put them there and restrict them to that scope. It would be neater
> than leaving them in the global scope or copypasting into each
> operator.

One problem with this proposal is that it hurts modularity, as there is
no distinction in the instance declarations between the definitions of
"instance-local" functions and those of class methods.

This means that you cannot add new methods to an existing class
(together with a default implementation in terms of the existing
methods) without potentially breaking the existing instances.

Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)

More information about the Haskell-prime mailing list