caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: sacerdot@cs.unibo.it
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Hiding public methods/friends methods
Date: Thu, 31 Jan 2002 09:09:12 +0900	[thread overview]
Message-ID: <20020131090912V.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <20020122182621.B8021@cs.unibo.it>

From: Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>

>  I would like to define a huge bunch of mutual recursive classes
>  with "friends methods". (The code is automatically generated, so
>  I don't mind very much an heavy coding style.)
> 
>  Of course, I can use the trick of abstracting the output type
>  of all the friends methods in the signature of the module.
>  Because I have lots of friends methods, though, the resulting
>  .mli I get is quite confusing.
> 
>  Another solution is creating "proxy" objects (see the code below).
>  But I fear the performance loss to be significant.
> 
>  Which solution is best (not withstanding performances) and why?
>  (Or which is a much better solution I don't see ;-)

Both are not very satisfactory, for the reasons you presented.
There is a long standing discussion about the bad consequences of
friend classes in C++ also.

> (* This is the interface I would like to have. *)
> module type MT =
>  sig
>   class type ct = object method get_c' : ct' method m : int end
>   and ct' = object method get_c : ct method m' : int end
>   class c : int -> ct
>   class c' : int -> ct'
>  end
> ;;

What you should really think about is whether you need to inherit from
these classes or not.
If you do not need to inherit (which is often the case in practice),
then you can avoid the problem by exporting only constructors:

module type MT =
 sig
  class type ct = object method get_c' : ct' method m : int end
  and ct' = object method get_c : ct method m' : int end
  val new_c : int -> ct
  val new_c' : int -> ct'
 end

These constructors can be defined in the implementation by:
  let new_c x = (new c x : c :> ct)

If you need to extend these classes (without overriding methods), you
can still define proxies afterwards. Proxies allowing method
overriding are much harder to define anyway.

Cheers,

Jacques Garrigue
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


      reply	other threads:[~2002-01-31  1:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-01-22 17:26 Claudio Sacerdoti Coen
2002-01-31  0:09 ` Jacques Garrigue [this message]

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=20020131090912V.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=sacerdot@cs.unibo.it \
    /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).