Version française
Home     About     Download     Resources     Contact us    
Browse thread
Request for example where type annotation are required
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Michael Wohlwend <micha-1@f...>
Subject: Re: [Caml-list] Request for example where type annotation are required
On Monday 19 September 2005 22:21, Christophe Raffalli wrote:
> I am looking for small ML examples (using variants, modules, objects,
> etc ...)
> Where one needs to write type information to be able to type-check the
> program with OCaml

As my example, this should be a try of linked objects (like dllist od extlib):

class ['a] node = object(self:'a)
    val mutable nx = nil
    method next = nx
    method set_next n = nx <- n
    method apply f  upto =
        f self; if self <> upto then self#next#apply f upto
end;;

it doesn't work; the problem is the "nil" objects to initalize it, i cannot 
get this right, there are allways typing errors. Can this be defined without 
type-annotations?

ahem, this one works easily :-) 

class ['a] node = object(self:'a)
    val mutable nx =(Obj.magic 0 : 'a) 
    method next = nx
    method set_next n = nx <- n
    method apply (f: 'a->unit)  upto =
        f self; if self <> upto then self#next#apply f upto
    initializer nx <- self
end;;
   
let a = new node;;
-------------------------------------------------------------

 Michael