Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Narrowing class's public interface
@ 2004-10-12 22:16 Tony Edgin
  2004-10-13  0:49 ` Jacques Garrigue
  0 siblings, 1 reply; 6+ messages in thread
From: Tony Edgin @ 2004-10-12 22:16 UTC (permalink / raw)
  To: caml-list

What is the reasoning behind Ocaml not allowing the public methods of a class 
to be hidden?  

Some times its nice for a class to have "global" public methods (Java's public 
methods) as well as public methods local to its module (Java's unprefixed 
methods).  This allows for progressive encapsulation, where more distance 
modules no very little about a class, while the more local modules know more 
about the class.  Inheritance could be used to approximate this, but 
sometimes subtyping makes more sense.

I apologize for my lack of clarity.  For those who couldn't understand my 
question, here's an example.

module RandomVariables =
struct
	class type rv_type =
	object
		method instance : float
		method scale_by : float -> rv_type
		method add : rv_type -> rv_type
	end

	class gaussian _stdev _mean =
	object
		method stdev = _stdev
		method mean = _mean

		method instance =  (* elided *)
			(** Returns a draw from the corresponding distribution.
			  *)

		method scale_by a = 
			new gaussian ((abs_float a)*._stdev) (a*._mean)

		method add (other : guassian) = 
			let sigma = sqrt (_stdev**2. +. other#stdev**2.)
			and mu = _mean +. other#mean
			in
				new gaussian sigma mu
	end
	
	(* Truncated *)
end

(* Bad code which I would like to use *)
class gaussian_rv = 
	(RandomVariables.gaussian float -> float -> RandomVariables.rv_type)

;;

cheers.

-- 
Tony Edgin
CARP

-------------------
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


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2004-10-13  7:35 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-12 22:16 [Caml-list] Narrowing class's public interface Tony Edgin
2004-10-13  0:49 ` Jacques Garrigue
2004-10-13  1:43   ` John Prevost
2004-10-13  2:27     ` skaller
2004-10-13  6:39       ` John Prevost
2004-10-13  7:34         ` skaller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox