caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Tom Ridge <tom.j.ridge+list@googlemail.com>
Cc: OCaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] Question about objects and method overriding
Date: Sun, 2 Feb 2014 21:43:59 +0900	[thread overview]
Message-ID: <B9A4FED7-AF2D-48D8-A4BB-6DCCEE08D434@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <CABooLwN_1vpyQAfA53cAiTk_5zACny1YZBt+dLd7h6WYyQiXQw@mail.gmail.com>

On 2014/02/02 20:55, Tom Ridge wrote:

> Dear caml-list,
> 
> With records, one can functionally update a field e.g. as
> 
> { r with some_field=new_value }
> 
> And new_value may, of course, be a function.
> 
> With objects, is there similar functionality? e.g. can I write something like
> 
> {{ myobj with method some_method=new_method }}
> 
> ?
> 
> Of course, I could copy the methods from myobj explicitly into a new
> object (and set some_method to new_method), but I might not know all
> the methods available on myobj, and even if I do this becomes
> textually extremely verbose.
> 
> Of course, new_method cannot directly refer to self etc. Basically I
> am using objects in a similar way to records, and would like to use
> this functional record update feature.
> 
> Thanks
> 
> Tom


You can copy an object updating a value field, not a method field:

class point (x:int) = object val x = x method get = x method bump dx = {< x = x+dx >} end

You can use this for methods too, by having them call a function value field.
However, this kind of update is only available inside methods, which requires some thinking
when designing your code.

I agree that what you suggest would be nice to have.
There is no majore technical difficulty, but there would be some overhead.

	Jacques Garrigue

  reply	other threads:[~2014-02-02 12:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-02 11:55 Tom Ridge
2014-02-02 12:43 ` Jacques Garrigue [this message]
2014-02-02 12:50   ` Tom Ridge
2014-02-03 10:35 ` Goswin von Brederlow
2014-02-03 12:24   ` Tom Ridge
2014-02-03 15:54     ` Goswin von Brederlow
2014-02-03 13:55   ` Alain Frisch
2014-02-03 15:12     ` Tom Ridge
2014-02-05 18:22 ` remy

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=B9A4FED7-AF2D-48D8-A4BB-6DCCEE08D434@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=tom.j.ridge+list@googlemail.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).