caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: Goswin von Brederlow <goswin-v-b@web.de>
Cc: OCaML List Mailing <caml-list@inria.fr>
Subject: Re: [Caml-list] question: what is the recommended use case of `val` in class type
Date: Tue, 5 Jul 2016 06:56:09 +0900	[thread overview]
Message-ID: <2B4F34AC-A05C-458B-BC95-C4406A27819F@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <20160704101918.GB7663@frosties>

On 2016/07/04 19:19, Goswin von Brederlow wrote:
> 
> On Sun, Jul 03, 2016 at 03:23:35PM +0900, Jacques Garrigue wrote:
>> Hi Hongo,
>> 
>> You may want to have a look at a small camlp4 syntax extension I wrote a long time ago,
>> which allows to do what you say.
>> Using it, one would write:
>> 
>>     val mutable x with accessor
>> 
>> to say that it can be accessed through the x and x_set methods.
>> 
>> The code is here:
>> http://www.math.nagoya-u.ac.jp/~garrigue/code/ocaml.html
>> 
>> Note that I didn’t try to support functional state changes, i.e. adding a method
>>    method x_upd x’ = {< x = x’ >}
>> 
>> Jacques
> 
> What's the point of x then? Wouldn't it make more sense to only expose
> the accessors so even derived classes will go through them instead of
> accessing x directly?

Maybe, if OCaml were an object-oriented language.
But the accessor approach doesn’t mix too well with multiple inheritance, whereas
ocaml instance variables do.

> Question: Can ppx attributes generate the accessors but also hide the
> "val mutable x" when no mli file is present?


Not so easy to do, because you need to write an explicit class interface to hide
the instance variable, and for this you need to write the type of the methods x and x_set.
Here is some code that does it, and could be translated into an extension:

class c (x : int) = object
   inherit (fun x -> object val mutable x = x method x = x method x_set y = x <- y end :  'a -> object method x : 'a method x_set : 'a -> unit end) x
end;;

One could generate the inherit line.

Jacques

  reply	other threads:[~2016-07-04 21:56 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-02 17:23 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-03  6:23 ` Jacques Garrigue
2016-07-04 10:19   ` Goswin von Brederlow
2016-07-04 21:56     ` Jacques Garrigue [this message]
  -- strict thread matches above, loose matches on Subject: below --
2016-07-05 13:00 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 17:02 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 17:07 ` Nicolas Ojeda Bar
2016-07-02 16:45 Hongbo Zhang (BLOOMBERG/ 731 LEX)
2016-07-02 16:59 ` Gabriel Scherer

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=2B4F34AC-A05C-458B-BC95-C4406A27819F@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=goswin-v-b@web.de \
    /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).