<div class="markdown-here-wrapper" id="markdown-here-wrapper-551119" style><p style="margin:1.2em 0px!important">On Tue Apr 29 2014 at 16:42:40, Lorenzo Tabacchini <a href="http://mailto:lortabac@gmx.com">lortabac@gmx.com</a> wrote:</p>
<p style="margin:1.2em 0px!important"></p><div class="markdown-here-exclude"><p></p><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">If you could have a single polymorphic "age" function for all types<br>
having an age, you could never have this runtime error, because the<br>
compiler would infer which "age" we are referring to.<br>
The GHC OverloadedRecordFields extension that Michael linked seems to do<br>
what I am looking for (<a href="http://www.well-typed.com/blog/84/" target="_blank">http://www.well-typed.com/<u></u>blog/84/</a>).<br></blockquote><p></p></div><p style="margin:1.2em 0px!important"></p>
<p style="margin:1.2em 0px!important">This is accurate. In the meantime avoid partiality as much as possible. If you must have an age accessor, better to define the lens explicitly.</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="language-haskell" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;background-color:rgb(248,248,248);white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,255)"><span class="module"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">module</span> Main <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">where</span></span>
<span class="import"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">import</span> Control.Applicative</span>
<span class="import"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">import</span> Control.Lens</span>
<span class="title" style="color:rgb(153,0,0);font-weight:bold">main</span> :: <span class="type">IO</span> ()
<span class="title" style="color:rgb(153,0,0);font-weight:bold">main</span> = <span class="keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> p = <span class="type">Child</span> <span class="number" style="color:rgb(0,153,153)">10</span>
<span class="keyword" style="color:rgb(51,51,51);font-weight:bold">in</span> print $ p ^. age
<span class="typedef"><span class="keyword" style="color:rgb(51,51,51);font-weight:bold">data</span> <span class="type">Person</span> = <span class="type">Child</span> <span class="type">Int</span> | <span class="type">Adult</span> <span class="type">Int</span></span>
<span class="title" style="color:rgb(153,0,0);font-weight:bold">age</span> :: <span class="type">Lens'</span> <span class="type">Person</span> <span class="type">Int</span>
<span class="title" style="color:rgb(153,0,0);font-weight:bold">age</span> k (<span class="type">Child</span> x) = <span class="type">Child</span> <$> k x
<span class="title" style="color:rgb(153,0,0);font-weight:bold">age</span> k (<span class="type">Adult</span> x) = <span class="type">Adult</span> <$> k x
</code></pre>
<p style="margin:1.2em 0px!important">Yeah it’s boilerplate. You could <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;background-color:rgb(248,248,248)">makeLenses</code> and then not export the automatically generated selector functions.</p>
</div>