caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Jordan W <jordojw@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Object Features
Date: Mon, 1 Dec 2014 09:49:32 +0000	[thread overview]
Message-ID: <CAAxsn=F54CpGmqUCD=hxp9=bPv5jLuGkpb8YyKPX4qjMUMuoEA@mail.gmail.com> (raw)
In-Reply-To: <CAPOA5_7BMBpK0g48upT4GqunPCC-JRcGkAg884X-6E9FbPjBpA@mail.gmail.com>

On 30 November 2014 at 04:24, Jordan W <jordojw@gmail.com> wrote:
> I understand that object punning on "functional updates" to objects was
> recently added to trunk. This is a nice consistency, but I haven't found a
> way to perform object punning on methods or values for object *expressions*.
>
>     let x = 10
>     let y = 20
>     let o = object
>       method x
>       method y
>     end

I wonder how far it's wise to take this idea.  It would be possible to
extend the approach to a lot of places where it's currently necessary
to write 'x = x'.  For example, we could extend module-level bindings
similarly:

   module (F : sig
                 val x : int
                 val y : int
               end) =
   struct
     open F
     let x
     let y
   end

(This approach breaks down for other binding forms, though: 'module
type S' and 'exception E' mean different things already, and 'type t'
has the wrong scoping.)

For records and labels punning is a clear improvement, because it's
available in both patterns and expressions.  It's quite important for
patterns to be succinct, and the extension to expressions is an
obvious symmetry.

For functional object updates things are less clear-cut, although I
think there's still an argument to be made in favour of punning.  The
form {< a = x; b = y >} is so similar to { a = x; b = y } that there's
no real reason not to support the same abbreviated form in both cases.

The argument for method punning is perhaps even weaker, not least
because you lose some of the power of methods with the proposed
abbreviated form, since the 'self' variable is not available.

> 3. Object matching.
>
>     let myFunction delta {<x; y; ..>} = x + y + delta
>
>     let myFunction delta o = match o with
>         {<x; y; .. >} -> x + y + delta
>
>
> This may be relatively easy to implement (my reasoning is that I believe it
> could even be solved at the parsing stage (not that it would be a good idea
> to do so)).

Right: this was one of the examples distributed with the 'patterns'
framework for user-defined extensions to pattern matching:

    https://code.google.com/p/ocaml-patterns/#Object_patterns

Without some such framework it's not entirely trivial to desugar
object patterns into existing syntax, since you have to be able to
both bind variables and "backtrack" (i.e. resume matching at the next
pattern) when patterns don't match.  Consider how you'd translate the
following, for example:

    function
      Some {< x = Some y >} -> e1
    | ...

You might be inclined to start off like this

    function
      Some o -> (match o#x with Some y -> e1 | ...

but then you need some way of returning to the next pattern in the
top-level match if 'o#x' doesn't match 'Some y'.

Anyway, this is a little off topic.  As you say, it wouldn't be a good
idea to treat object patterns during parsing.

  parent reply	other threads:[~2014-12-01  9:49 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-30  4:24 Jordan W
2014-11-30 13:45 ` Philippe Wang
2014-12-08  9:34   ` Goswin von Brederlow
2014-12-08 10:06     ` Gabriel Scherer
2014-11-30 15:49 ` Gerd Stolpmann
2014-12-01 10:09   ` Alain Frisch
2014-12-26 23:45     ` Jordo
2014-12-01  9:49 ` Jeremy Yallop [this message]
2014-12-08  9:35   ` Goswin von Brederlow
2014-11-30 15:53 Damien Guichard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=F54CpGmqUCD=hxp9=bPv5jLuGkpb8YyKPX4qjMUMuoEA@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=jordojw@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).