[MLton] Slightly simpler functional record update

Stephen Weeks MLton@mlton.org
Fri, 12 Aug 2005 11:07:22 -0700


> > fun set f =
> >    let
> >       fun r (a, b, c) = {a = a, b = b, c = c}
> >       fun t {a, b, c} = (a, b, c)
> >    in
> >       makeSetter3 (r, r, t) f
> >    end
> 
> I prefer to write the above as
> 
>  fun set f =
>     let
>        fun r (v1, v2, v3) = {a = v1, b = v2, c = v3}
>        fun t {a = v1, b = v2, c = v3} = (v1, v2, v3)
>     in
>        makeSetter3 (r, r, t) f
>     end
> 
> This has the following minor advantages:
> - The names of fields are only mentioned twice.
> - The names of fields only appear syntactically as fields.
>   This means that the code is invariant with respect to
>   fixity declarations on the field names.
> - The patterns/constructions used in the definition of r
>   and t are identical. This makes for slightly faster
>   manual editing, if necessary.

The second point I agree with, although I also agree with its
"minorness" :-).  The first and third may be slightly advantageous if
one is writing the set functions by hand, although find-replace would
seem to work well.  In any case, as you mention, this code is likely
auto-generated, in which case due to readability, I think my code is
preferable.

> BTW, I have started an e-lisp file named `esml-gen.el' as a place
> for SML code generation functions. I have a function by the name
> `esml-gen-fru-setter' which generates a `set' function given a simple
> record pattern or a type. E.g. the above code can be generated in Emacs
> by typing `M x esml-gen-fru-setter {a, b, c}"'. If you think that it
> is worth adding to the SVN, I can post it.

Sure.  No hurry though.  Perhaps to simplify things we should add you
as a committer, at least to ide/emacs/*.

BTW, for easier typing, how about "M x esml-gen-fru-setter a b c"?