# Default method implementation

### From HaskellWiki

(Difference between revisions)

(some conclusions from my mail to Haskell-Cafe) |
(no new super-classes) |
||

Line 5: | Line 5: | ||

* Check, that default implementations does not introduce type class dependencies, that are not necessary otherwise |
* Check, that default implementations does not introduce type class dependencies, that are not necessary otherwise |
||

: E.g. if you have a <hask>Monad</hask> constraint, but no <hask>Functor</hask> constraint, then use <hask>liftM</hask> instead of <hask>fmap</hask>. |
: E.g. if you have a <hask>Monad</hask> constraint, but no <hask>Functor</hask> constraint, then use <hask>liftM</hask> instead of <hask>fmap</hask>. |
||

− | * If you have the choice, whether to implement the method default by custom code or by calling other methods of the same class, call methods of the same class. This reduces the amount of implementation work for class instances. |
+ | * If you have the choice, whether to implement the method default by custom code or by calling other methods of the same class, call methods of the same class. This reduces the amount of implementation work for class instances. It also makes it more probable, that no extra super-classes are needed. |

: E.g. the default methods of the class providing <hask>divMod</hask>, <hask>div</hask>, and <hask>mod</hask> should implement the defaults, where <hask>divMod</hask> calls <hask>div</hask> and <hask>mod</hask> and vice versa. |
: E.g. the default methods of the class providing <hask>divMod</hask>, <hask>div</hask>, and <hask>mod</hask> should implement the defaults, where <hask>divMod</hask> calls <hask>div</hask> and <hask>mod</hask> and vice versa. |
||

: Do not try to implement <hask>mod</hask> by repeated subtraction or so. |
: Do not try to implement <hask>mod</hask> by repeated subtraction or so. |

## Revision as of 17:12, 13 November 2006

It's always a design question, whether to provide default implementions of methods of Type classes and how to desing them.

Here are some suggestions:

- Make only trivial default implementations.
- Check, that default implementations does not introduce type class dependencies, that are not necessary otherwise

- E.g. if you have a constraint, but noMonadconstraint, then useFunctorinstead ofliftM.fmap

- If you have the choice, whether to implement the method default by custom code or by calling other methods of the same class, call methods of the same class. This reduces the amount of implementation work for class instances. It also makes it more probable, that no extra super-classes are needed.

- E.g. the default methods of the class providing ,divMod, anddivshould implement the defaults, wheremodcallsdivModanddivand vice versa.mod
- Do not try to implement by repeated subtraction or so.mod

- Document which methods must be implemented at least.

- E.g. "instances must implement or (divModanddiv)"mod

- Instance implementations should not call other methods of the same class with respect to instantiated type.

## See also

http://www.haskell.org/pipermail/haskell-cafe/2006-November/019329.html