Version française
Home     About     Download     Resources     Contact us    
Browse thread
Type_of?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jeremy Yallop <jeremy.yallop@e...>
Subject: Re: [Caml-list] Type_of?
Dario Teixeira wrote:
> So, imagine I have a module with only two functions.  The first, "make_doc",
> uses XHTML.M and its signature is therefore quite complex.  This is what
> "ocamlc -i" tells me:
[...]
> The second function, "unpickle_doc", uses the Marshal module to deserialise
> from a string a previously pickled doc.  This is the definition of this
> function:  (note that I've used copy & paste of the previous output of
> "ocamlc -i" to provide the explicit type annotation)
[...]
> Now, this works fine.  It is however error prone, since if the signature
> of make_doc changes somewhat and I forget to update the type annotation in
> unpickle_doc, then I get a nasty runtime segfault.  So this is my question:
> since the return type of make_doc is known at compile-time, is there any
> way I can tell the compiler that the return type of unpickle_doc should be
> the same as make_doc's?

One way is to use both functions in a context which requires the return 
types to be the same.  For example, you could use both functions to 
create the elements of a list:

   let rec unpickle_doc str =
     let _unify_return_types _ = [unpickle_doc ""; make_doc ""] in
       Marshal.from_string str 0

Jeremy.