caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: alex@baretta.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Protected methods
Date: Sat, 20 Jul 2002 10:31:17 +0900	[thread overview]
Message-ID: <20020720103117U.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: <3D37E364.5060607@baretta.com>

From: Alessandro Baretta <alex@baretta.com>

> How about the following pseudocode? Is it sensible/viable?
> 
> let module M : sig
>    class type public = object <public_methods> end
>    val make_public : unit -> public
> end = struct
>    class type public = object <public_methods> end
>    class protectd =
>      object (self : #public)
>      <public_methods>
>      <protected_methods>
>    end
>    let make_public () -> (new protected :> public)
> end

Actually, after a more thorough look at your code, I'm not sure of
what you're trying to achieve with it.  Applied directly, it seems
that it would give you no more than private methods: you can not use
them outside of the class.

And I've found a better idiom, that should do about anything you want.

module M : sig
  type hidden
  class type public = object < public methods >  method full : hidden end
  val make_public : unit -> public
  class virtual protected : object ('a)
    < public methods >
    < protected methods >
    method full : 'a
  end
end = struct
  class protected = object (self)
    < public methods >
    < protected methods >
    method full = self
  end
  type hidden = protected
  class type public = object < public methods >  method full : hidden end
  let make_public () = (new protected : protected :> public)
end

The point is that now you can access the protected methods while
your code appears in the same module, through the full method (which
just returns self, but with its protected methods accessible), but not
from outside the module (hidden is abstract).

Note also how I exported protected as virtual: this way you cannot
create objects from it, but you can still use it through inheritance.
If you don't need to inherit, you don't have to export it though, and
then you don't have to distinguish protected and hidden either.

Here is a concrete instance of that, to verify the type checking:

module M : sig
  type hidden
  class type public = object method pub : int  method full : hidden end
  val make_public : unit -> public
  val call_prot : public -> int
  class virtual protected : object ('a)
    method pub : int
    method prot : int
    method full : 'a
  end
end = struct
  class protected = object (self)
    method prot = 1
    method pub = self#prot
    method full = self
  end
  type hidden = protected
  class type public = object method pub : int  method full : hidden end
  let make_public () = (new protected : protected :> public)
  let call_prot (o : public) = o#full#prot
end

Jacques Garrigue
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2002-07-20  1:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-18 10:42 Alessandro Baretta
2002-07-18 11:01 ` Gerd Stolpmann
2002-07-18 11:44   ` Alessandro Baretta
2002-07-19  8:50     ` Jacques Garrigue
2002-07-19 10:01       ` Alessandro Baretta
2000-07-20  0:46         ` Jacques Garrigue
2002-07-20  7:41           ` Alessandro Baretta
2002-07-20  1:31         ` Jacques Garrigue [this message]
2002-07-20  7:48           ` Alessandro Baretta
2002-07-20 22:48 ` Dmitry Bely
2002-07-20 23:08   ` Brian Smith
2002-07-22  3:37     ` OCaml's OO design " Jacques Garrigue
2002-07-22  4:20       ` John Prevost
2002-07-20 23:54   ` Alessandro Baretta
2002-07-21  7:52     ` Dmitry Bely
2002-07-21 13:14       ` Alessandro Baretta

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=20020720103117U.garrigue@kurims.kyoto-u.ac.jp \
    --to=garrigue@kurims.kyoto-u.ac.jp \
    --cc=alex@baretta.com \
    --cc=caml-list@inria.fr \
    /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).