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] let or val in objects
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2002-04-01 (22:54)
From: james woodyatt <jhw@w...>
Subject: Re: [Caml-list] let or val in objects
On Monday, April 1, 2002, at 01:28 AM, John Prevost wrote:
> On Sun, Mar 31, 2002 at 10:40:51PM +0200, Richard Nyberg wrote:
>> Hello, I'm a bit confused regarding let bindings and nonmutable vals in
>> objects. Are there any difference between the classes a and b? or are 
>> they
>> equivalent?
>> Like this:
>> class a fd =
>>  let is = in_channel_of_descr fd in object ... end
>> class b fd =
>>  object val is = in_channel_of_descr fd ... end;;
> In the second case, "is" is a named value that's part of the 
> object--inheriting
> classes can access its value, and if it is mutable, change the value.
> In the first case, "is" is a variable in the closure of the methods in
> the object.  Inheriting classes may not access its value in any way, 
> including
> (of course) modifying it if it has a mutable component.

I was following this thread in the hopes that I might learn whether 
there is any difference (in space or time) between values hidden by 
class signature matching and values in the methods closure.

For example, I wanted to know if there is any substantial savings to be 
gained by choosing one of the following forms over the other:

	(* assume:
	   type t
	   val f: t -> unit

	(* 1 *)
	class foo (x : t) = object method bar = f x end

	(* 2 *)
	class type foo_t = object method bar: unit end
	class foo x : foo_t = object val x' = x method bar = f x' end

As far as I can tell, these two forms are semantically equivalent.

So, I looked to see if the compiler generates the same output code for 
each case.  Cursory examination of the output of ocamlopt -S on my Mac 
OS X unit seems to show that it does.

I suspect the second case is optimized into the first case.  Or 

j h woodyatt <>

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