Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organize Haskell modules with instances: stick to data type vs type class?
    text
    copied!<p>The general question is which module structure is more convenient when adding instances for existing objects? Which pros and cons there are?</p> <p>Let's say I want to add NFData instance for Seq type. I can place it in:</p> <ul> <li><code>Data.Sequence.Extra</code> (as the same thing is done in the <a href="https://github.com/coreyoconnor/vty/blob/master/src/Data/Sequence/Extra.hs">vty</a> package)</li> <li><code>Data.Sequence.Instances.NFData</code> (more precise)</li> <li><code>Control.DeepSeq.Instances</code></li> <li><code>Control.DeepSeq.Instances.Sequence</code></li> </ul> <p>It's the case when I don't own neither the type class nor the data type. The other common situation is when I own a type type class and want to add instances for data type(s) from some "heavy" package from Hackage, like OpenGL. Let's say the type class I designed is very light and has no direct relation to OpenGL. I don't want my type class depend on "heavy" package, so I want to place OpenGL instances in a separate module (it's my intuitive feeling, if you have other opinion, let's discuss it). So, what this module should be:</p> <ul> <li><code>MyClass.Instances.OpenGL</code></li> <li><code>Graphics.Rendering.OpenGL.Extra</code> (together with instances for other classes)</li> <li><code>Graphics.Rendering.OpenGL.Instances.MyClass</code></li> </ul> <p>What is more flexible solution? At some point OpenGL can be replaced with other library, or MyClass can be replaced too. Are there any subtle nuances?</p> <p>Also, which scheme is better if choose <code>MyClass.Instances</code> variant:</p> <ul> <li><code>MyClass.Class</code> module for the class itself and basic instances and <code>MyClass</code> module reexports it (and maybe <code>MyClass.Instances</code>)</li> <li><code>MyClass</code> module for the class and basic instances, and <code>MyClass.All</code> reexports everything</li> <li><code>MyClass</code> module for the class and basic instances and no module for reexporting.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload