<div dir="ltr">The suggestion of parameterizing on a functor would be good, however there&#39;s another approach I&#39;ve often seen (although it&#39;s not quite what you&#39;ve asked for).  You can leave your config datatype alone, but instead of making it a monoid have your configuration parsers return functions with the type (Cfg -&gt; Cfg).  You can wrap these functions in Endo to get a monoid, combine them together, and then apply that function to the default configuration.</div>
<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Jul 17, 2013 at 4:57 AM, Michael Orlitzky <span dir="ltr">&lt;<a href="mailto:michael@orlitzky.com" target="_blank">michael@orlitzky.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have a common pattern in my command-line programs; I start out with a<br>
configuration data type, which over-simplified looks like:<br>
<br>
  data Cfg = Cfg { verbose :: Bool }<br>
<br>
Now, there&#39;s usually a default configuration,<br>
<br>
  default :: Cfg<br>
  default = Cfg False<br>
<br>
The user can override the defaults one of two ways, either via a config<br>
file, or from the command-line. If both are specified, the command-line<br>
takes precedence. The way I do this is with,<br>
<br>
  data OptionalCfg = OptionalCfg { verbose :: Maybe Bool }<br>
<br>
And then I define I Monoid instance for OptionalCfg which lets me merge<br>
two ofthem. Once the two OptionalCfgs are merged, I merge *that* with<br>
the default Cfg.<br>
<br>
This all works great, except that when there&#39;s 20 or so options, I<br>
duplicate a ton of code in the definition of OptionalCfg. Is there some<br>
pre-existing solution that will let me take a Cfg and create a new type<br>
with Cfg&#39;s fields wrapped in Maybe?<br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>