caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] New Object Creation
@ 2002-07-07 20:12 Gaurav Chanda
  2002-07-07 21:55 ` Alessandro Baretta
  0 siblings, 1 reply; 2+ messages in thread
From: Gaurav Chanda @ 2002-07-07 20:12 UTC (permalink / raw)
  To: caml-list

Hello 

I created 2 files : point.ml and point.mli. The first one contained a simple class definition.

class point =
        object
                value mutable x = 0;
                method addunit v = v+1 ;
        end;
The second one contained 

class type point =
        object
                value mutable x : int;
                method addunit : int -> int ;
        end;

I used this class in another file, test.ml which contained 

open Point;

value parse t = match t with
  [ "" -> let a = new point in
          ()
  |  _ -> ()
  ];

My compilation command was: 
ocamlopt -pp "camlp4r pa_extend.cmo" -I +camlp4  -c point.mli point.ml test.ml

However, on compilation I got an error message:

File "t2.ml", line 4, characters 18-27:
Unbound class point
make: *** [code] Error 2

I am not able to create a new point object from outside the file. Could you help me solve this problem ?

Gaurav



_____________________________________________________
Supercharge your e-mail with a 25MB Inbox, POP3 Access, No Ads
and NoTaglines --> LYCOS MAIL PLUS.
http://www.mail.lycos.com/brandPage.shtml?pageId=plus 
-------------------
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] 2+ messages in thread

* Re: [Caml-list] New Object Creation
  2002-07-07 20:12 [Caml-list] New Object Creation Gaurav Chanda
@ 2002-07-07 21:55 ` Alessandro Baretta
  0 siblings, 0 replies; 2+ messages in thread
From: Alessandro Baretta @ 2002-07-07 21:55 UTC (permalink / raw)
  To: Ocaml

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-07 21:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-07-07 20:12 [Caml-list] New Object Creation Gaurav Chanda
2002-07-07 21:55 ` Alessandro Baretta

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