[Haskell-cafe] Wrapping all fields of a data type in e.g. Maybe

Michael Orlitzky michael at orlitzky.com
Tue Jul 16 22:57:59 CEST 2013


I have a common pattern in my command-line programs; I start out with a
configuration data type, which over-simplified looks like:

  data Cfg = Cfg { verbose :: Bool }

Now, there's usually a default configuration,

  default :: Cfg
  default = Cfg False

The user can override the defaults one of two ways, either via a config
file, or from the command-line. If both are specified, the command-line
takes precedence. The way I do this is with,

  data OptionalCfg = OptionalCfg { verbose :: Maybe Bool }

And then I define I Monoid instance for OptionalCfg which lets me merge
two ofthem. Once the two OptionalCfgs are merged, I merge *that* with
the default Cfg.

This all works great, except that when there's 20 or so options, I
duplicate a ton of code in the definition of OptionalCfg. Is there some
pre-existing solution that will let me take a Cfg and create a new type
with Cfg's fields wrapped in Maybe?




More information about the Haskell-Cafe mailing list