Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-10-12 (22:16)
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 =
	class type rv_type =
		method instance : float
		method scale_by : float -> rv_type
		method add : rv_type -> rv_type

	class gaussian _stdev _mean =
		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
				new gaussian sigma mu
	(* Truncated *)

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



Tony Edgin

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: