Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Narrowing class's public interface
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Tony Edgin <edgin@s...>
Subject: [Caml-list] Narrowing class's public interface
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