Extensible records & mandatory/optional record fields

Harendra Kumar harendra.kumar at gmail.com
Tue Feb 14 18:50:33 UTC 2017


In my application, I want some of the fields in a record to have default
values while others must always be specified by the user.  I can implement
this using the rawr records library (http://hackage.haskell.org/package/rawr)
like so:

def :: ("b" := String) -> R ("a" := Int, "b" := String)
def t = (R (#a := 0) :*: R t) -- merge with defaults

let r = def (#b := "hello")

Here field "a" has a default value (i.e. it is optional when "def" is used)
but field "b" does not have a default value and has to be explicitly
specified by the user. This is made possible by record merging feature of
rawr, making the record extensible.

I could not find an equivalent way of achieving this using overloaded
records (http://hackage.haskell.org/package/overloaded-records). Is record
merging possible or can be made possible using overloaded-records or with
the ghc overloaded records poposal? If not, is there a plan to make
something like this possible in future? Or will this never be possible with
the in built record syntax?

Note that, this is handy to simulate mandatory and optional keyword
arguments to a function by passing them in the form of a record. Something
which is available in other languages like python.

-harendra
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-devs/attachments/20170215/6a4f2dff/attachment.html>


More information about the ghc-devs mailing list