[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Arnaud Spiwack <Arnaud.Spiwack@l...> |
| Subject: | Re: [Caml-list] Type_of? |
Hello, How about giving it a name ? For instance: type 'a make_doc_type = 'a XHTML.M.elt list constraint 'a = [< `Address | `Blockquote | `Del | `Div | `Dl | `Fieldset | `Form | `H1 | `H2 | `H3 | `H4 | `H5 | `H6 | `Hr | `Ins | `Noscript | `Ol | `P | `PCDATA | `Pre | `Script | `Table | `Ul > `Blockquote `H1 `P ] (this is untested, might contain a few dozens syntax errors, but it's the idea). Arnaud Spiwack Dario Teixeira a écrit : > Hi, > > I'm using the XHTML.M module from the Ocsigen project to generate valid > XHTML pages. It makes heavy use of polymorphic variants, and as such the > types produced can be quite verbose and complex. In practice, however, one > is rarely confronted with them (thank goodness for latent typing and type > inference!). There is one exception, though: when unmarshalling, one must > explicitly provide the return type. And this is the context for my question. > > 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: > > val make_doc: string -> > [< `Address | `Blockquote | `Del | `Div | `Dl | `Fieldset | `Form | `H1 > | `H2 | `H3 | `H4 | `H5 | `H6 | `Hr | `Ins | `Noscript | `Ol | `P | > `PCDATA > | `Pre | `Script | `Table | `Ul > `Blockquote `H1 `P ] > XHTML.M.elt list > > > 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) > > let unpickle_doc str : > [< `Address | `Blockquote | `Del | `Div | `Dl | `Fieldset | `Form | `H1 > | `H2 | `H3 | `H4 | `H5 | `H6 | `Hr | `Ins | `Noscript | `Ol | `P | > `PCDATA > | `Pre | `Script | `Table | `Ul > `Blockquote `H1 `P ] > XHTML.M.elt list > = Marshal.from_string str 0 > > > 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? > > Thank you for your help! > Dario Teixeira > > > > > > ___________________________________________________________ > Support the World Aids Awareness campaign this month with Yahoo! For Good http://uk.promotions.yahoo.com/forgood/ > > _______________________________________________ > Caml-list mailing list. Subscription management: > http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list > Archives: http://caml.inria.fr > Beginner's list: http://groups.yahoo.com/group/ocaml_beginners > Bug reports: http://caml.inria.fr/bin/caml-bugs > >