[Haskell-cafe] Idiomatic Haskell equivalent of "keyword arguments" to functions

Paul Moore p.f.moore at gmail.com
Fri Dec 29 17:26:29 EST 2006


I'm thinking around the design of a couple of things, and am hitting
an issue which I know how I would solve in Python, but I'm not sure
what a good idiomatic Haskell approach would be.

The problem is that I am trying to write a function which takes a
rather large number of arguments, many of which are optional (ie, have
sensible defaults). The canonical example of this is creating a GUI
window, which involves a lot of style options, most of which would
typically be left to default. In Python, this type of interface is
often handled either as a function with many keyword arguments, or as
a mutable object which has attributes set, and then a method called to
handle the function call. Neither of these approaches seems plausible
in Haskell.

I looked at wxHaskell for inspiration - its approach (button f [text
:= "Quit", on command := close f]) looks quite readable, but slightly
unusual (to me) for Haskell. It also seems fairly complex to implement
(ie, my head hurt when I tried to follow the types involved, but maybe
that's just because it's getting late :-))

To make things concrete, the example I'm really thinking of is a "send
an email" function, which would take a subject, a body, a list of
recipients, optional lists of cc and bcc recipients, an optional
mailserver (default localhost), an optional port (default 25), and
possibly optional authentication details. I found a couple of Haskell
modules implementing a SMTP client, but they both just used a list of
positional parameters, which I'm not really happy with. At the very
least, I'd like to wrap them in a nicer interface for my code.

I'd appreciate any ideas about how to think of this sort of problem -
I'm pretty sure that what I need to do is think differently about the
issue, rather than just mechanically translating the code I'd write in
Python. But I don't really know how. Any pointers would be very
helpful!

Thanks,
Paul.


More information about the Haskell-Cafe mailing list