[MLton] A variation of functional record update notation

Vesa Karvonen vesa.karvonen@cs.helsinki.fi
Tue, 9 Aug 2005 03:05:28 +0300


Quoting Stephen Weeks <sweeks@sweeks.com>:
> Nice.  I've been using a similar approach recently in some code where
> I wanted a convenient syntax for imperative record update.  Here's an
> example.  Suppose we have some object type X.t, where such objects
> have a settable "n", "s", and "x" field.  Define the setters, as you
> did, to be curried and to take the object last.
[...]
>       val () = X.s >| S.n 13 & S.s "foo" & S.x 17.0

That's neat. I'll have to keep it in mind.

> An alternative to introducing the new infix "&" operator is to change
> the type of the setters to return the object.  Then, one could just
> use pipe, as in
> 
>       val _ = X.s >| S.n 13 >| S.s "foo" >| S.x 17.0

In addition to what you said about collecting attributes together, I think
that returning a unit is good, because it informs the programmer that the
operation is imperative.

> > [...] change the FRU-page to use the piping notation?
> 
> I have no objection to the page being changed.  I think the new
> approach is more useful too.  The fewer infixes the better.

Great. I'll update the page. I have also made an interactive elisp function
to generate `set'-functions. When invoked, the `esml-gen-fru-setter' function
prompts for a record pattern. It then generates a `set' function for the
record. For example, given "{first, rest}" it generates:

fun set f v s =
    let
       fun r (first, rest) = {first = first, rest = rest}
       fun t {first, rest} = (first, rest)
    in
       Records.makeSetter2 (r, r, t) f v s
    end

Speaking of Emacs, I recently watched (most of) a SLIME (The Superior Lisp
Interaction Mode for Emacs) video made by Marco Baringer:

  http://bc.tech.coop/blog/050728.html

Pretty cool, IMO. Much of the SLIME functionality would also make sense for
an SML mode.

-Vesa Karvonen