Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Generally speaking, <code>Any</code> types aren't very useful. Consider: If you make a polymorphic list that can hold anything, what can you do with the types in the list? The answer, of course, is nothing - you have no guarantee that there is any operation common to these elements.</p> <p>What one will typically do is either:</p> <ol> <li><p>Use <a href="http://en.wikibooks.org/wiki/Haskell/GADT" rel="noreferrer">GADTs</a> to make a list that can contain elements of a specific typeclass, as in:</p> <pre><code>data FooWrap where FooWrap :: Foo a =&gt; a -&gt; FooWrap type FooList = [FooWrap] </code></pre> <p>With this approach, you don't know the concrete type of the elements, but you know they can be manipulated using elements of the <code>Foo</code> typeclass.</p></li> <li><p>Create a type to switch between specific concrete types contained in the list:</p> <pre><code>data FooElem = ElemFoo Foo | ElemBar Bar type FooList = [FooElem] </code></pre> <p>This can be combined with approach 1 to create a list that can hold elements that are of one of a fixed set of typeclasses.</p></li> <li><p>In some cases, it can be helpful to build a list of manipulation functions:</p> <pre><code>type FooList = [Int -&gt; IO ()] </code></pre> <p>This is useful for things like event notification systems. At the time of adding an element to the list, you bind it up in a function that performs whatever manipulation you'll later want to do.</p></li> <li><p>Use <code>Data.Dynamic</code> (not recommended!) as a cheat. However, this provides no guarantee that a specific element can be manipulated at all, and so the above approaches should be preferred.</p></li> </ol>
 

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