caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] let or val in objects
@ 2002-03-31 20:40 Richard Nyberg
  2002-04-01  9:28 ` John Prevost
  2002-04-02  4:08 ` Brian Rogoff
  0 siblings, 2 replies; 4+ messages in thread
From: Richard Nyberg @ 2002-03-31 20:40 UTC (permalink / raw)
  To: caml-list

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
  .
  .
  Use is somewhere
  .
  .
  end;;

class b fd =
  object
    val is = in_channel_of_descr fd
    .
    .
    Use is somewhere
    .
    .
    end;;

TIA!
	-Richard
-------------------
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] 4+ messages in thread

* Re: [Caml-list] let or val in objects
  2002-03-31 20:40 [Caml-list] let or val in objects Richard Nyberg
@ 2002-04-01  9:28 ` John Prevost
  2002-04-01 22:54   ` james woodyatt
  2002-04-02  4:08 ` Brian Rogoff
  1 sibling, 1 reply; 4+ messages in thread
From: John Prevost @ 2002-04-01  9:28 UTC (permalink / raw)
  To: Richard Nyberg, caml-list

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.

Using lets outside of the object defintion can be very useful when you wish
to have private values which inheriting classes may not access.  (For example,
in order to preserve some invariant.)  However, you may note that a
let-bound variable (if it is a function) may not itself access the contents
of the object.

So they are fairly significantly different.  :)  The normal use is to
let-bind something like an input_stream, which your methods can access but
inheriting methods cannot.  Then inheriting objects have no choice but to
use your accessor functions to access the stream.

John.
-------------------
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] 4+ messages in thread

* Re: [Caml-list] let or val in objects
  2002-04-01  9:28 ` John Prevost
@ 2002-04-01 22:54   ` james woodyatt
  0 siblings, 0 replies; 4+ messages in thread
From: james woodyatt @ 2002-04-01 22:54 UTC (permalink / raw)
  To: John Prevost; +Cc: Richard Nyberg, caml-list

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


--
j h woodyatt <jhw@wetware.com>

-------------------
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] 4+ messages in thread

* Re: [Caml-list] let or val in objects
  2002-03-31 20:40 [Caml-list] let or val in objects Richard Nyberg
  2002-04-01  9:28 ` John Prevost
@ 2002-04-02  4:08 ` Brian Rogoff
  1 sibling, 0 replies; 4+ messages in thread
From: Brian Rogoff @ 2002-04-02  4:08 UTC (permalink / raw)
  To: Richard Nyberg; +Cc: caml-list

One place where you may notice that lets are used is when you have a
bunch of instance variables that depend on other instance variables for
their definition. For instance this code (not checked, posting from
wife's OCamlless machine :()

class foo = object val s = "test" val l = String.length s end

won't compile because of the dependence of l on s.

This example is clearly contrived, but if you do GUI programming this sort
of situation occurs frequently.

-- Brian
-------------------
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] 4+ messages in thread

end of thread, other threads:[~2002-04-02 20:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-31 20:40 [Caml-list] let or val in objects Richard Nyberg
2002-04-01  9:28 ` John Prevost
2002-04-01 22:54   ` james woodyatt
2002-04-02  4:08 ` Brian Rogoff

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).