Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] New Object Creation
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] New Object Creation
Gaurav Chanda wrote:
> class point =
>         object
>                 value mutable x = 0;
>                 method addunit v = v+1 ;
>         end;
> 
> class type point =
>         object
>                 value mutable x : int;
>                 method addunit : int -> int ;
>         end;

Are you sure this class does what you expect it to? Member 
variable x gets initialized to 0, cannot be set to anything 
else, nor can its value be fetched. Furthermore, method 
addunit could be redefined as a simple function:
# let addunit x = x + 1;;

BTW, please try to post correct code. You should have written

class type point :
	object
		val mutable x : int
		method addunit : int -> int
	end

class point =
	object
		val mutable x = 0
		method addunit v = v + 1
	end

You have misunderstood the use of class interfaces. The 
point class which you define in point.ml *is not* the 
implementation of the interface point in point.mli. Also, 
module point, whose interface is defined in point.mli, does 
not export a class point, only *class interface* point.

Here is how to separate class interfaces from class 
implementations. Copy the following in point.mli:
 > class type point_sig =
 >	object
 > 
	val mutable x : int
 > 
	method addunit : int -> int
 >	end
 > class point : point_sig

Copy the following in point.ml:
 > class type point_sig =
 >	object
 > 
	val mutable x : int
 > 
	method addunit : int -> int
 >	end
 > class point : point_sig =
 >	object
 > 
	val mutable x = 0
 > 
	method addunit v = v+1
 >	end

Alex

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