[Haskell-cafe] Avoiding boilerplate retrieving GetOpt cmd line args

Neil Mitchell ndmitchell at gmail.com
Fri Jul 27 10:03:38 EDT 2007


Hi

Why not:

> data Flag
>   = Filter String
>   | DateFormat String
>   | DocStart String
>   | DocEnd String

Becomes:

data Flag = Flag Key String
data Key = Filter | DateFormat | DocStart | DocEnd

getString :: Flag -> Key -> String
getString (Flag x y) key = if key == x then y else ""

You can easily extend this to defaults:

defaults = [(DocStart,"1")]

then lookup, instead of just "" as the else clause.

If you have to use Data/Typeable you will no longer be writing
portable Haskell, and while they are great, they aren't the thing to
use here.

Thanks

Neil


More information about the Haskell-Cafe mailing list