Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Protected methods
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Garrigue <garrigue@k...>
Subject: Re: [Caml-list] Protected methods
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