Records in Haskell

David Waern david.waern at gmail.com
Sat Jan 14 20:49:41 CET 2012


2012/1/14 Johan Tibell <johan.tibell at gmail.com>:
> On Fri, Jan 13, 2012 at 3:52 PM, Simon Peyton-Jones
> <simonpj at microsoft.com> wrote:
>> I know of no proposal that advocates only (A).  It seems that we are agreed
>> that we must make use of types to disambiguate common cases.
>
> I will try to make the case for (A), just so it has been put on the table.
>
> Proposal
> =========
>
> The proposal is to implement
> http://hackage.haskell.org/trac/ghc/wiki/Records/NameSpacing without
> any of the type resolution parts. I think this approach is dismissed a
> bit to easily on the wiki page above:
>
>    "We have name-spaces, but it is hard to see how this is better
> than the current practice of adding prefixes to record fields: data
> Record = Record { recordA :: String }"
>
> There are (at least) three benefits of using namespaces (e.g.
> 'Record.a') rather than ad-hoc prefixes (e.g. 'recordA'):
>
>  * You can use a type synonym to abbreviate the namespace part (as
> shown on the wiki page.)
>
>  * If there's no ambiguity you don't need to use a namespace (e.g. you
> can use 'a' instead of 'Record.a').
>
>  * The namespace name is predictable (e.g. <Typename>.<fieldname>)
> while ad-hoc prefixes tend to use different conventions e.g. the whole
> record name (e.g. 'recordA') or some abbreviation thereof (e.g.
> 'rcrdA'.)
>
> The main argument for this approach is its simplicity; it's simple to
> understand for users and (hopefully) simple to implement.

At work we have extended the OCaml compiler with the same mechanism,
to solve the same problem. I find it quite annoying to use, as you
tend to try without a namespace name first, and if you get a compile
error you add one. I'd prefer a system which just infers the right
selector - less time spent fighting with the compiler!

David



More information about the Glasgow-haskell-users mailing list