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
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1996-05-28 (09:31)
From: ESPERET PHILIPPE <100060.2106@c...>
Subject: Oo.copy

%  ocaml1.tex 25-5-1996 (23h:16)

Philippe Esperet (France)
Fax : (33-1) 46 56 83 68

   I cannot understand how to access a fresh copy of an object with
O'Caml (Oo seems normally to copy only pointers).  Thank you if someone
can explain me how to get such a copy (please see below for an example).

   One related problem is : the documentation for O'Caml exists but is not
so thick ; it could be useful to have a list where true beginners in
OCaml might ask their questions and find examples.


#use "C:/OCAML/esperet/";;

class virtual 'a matrix(sz,init:int*'a) as self=
  val size=sz
  val m=Array.create_matrix sz sz init
  val zero=init 
  virtual printa:'a -> unit
  (*method copy()=new matrix(size,zero)
   forbidden: One cannot create instances of the virtual class matrix*)
  method m=m
  method set(i,j,nval:int*int*'a)= m.(i).(j) <- nval
  method build(f:int->int->'a)=for i=0 to size-1 do
                                 for j=0 to size-1 do 
                                   m.(i).(j)<- f i j done done;self
  method print()=for i=0 to size-1 do
                   for j=0 to size-1 do 
                     self#printa m.(i).(j);
                     print_char ';' done; 
                   print_newline() done

class int_matrix (sz:int)=
  inherit (int) matrix (sz,0)
  method printa=print_int

let m=new int_matrix 3;;
(m#build (fun i j->i+j))#print();;
let n=Oo.copy m;;
(*let nn=m#copy();;*)