From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id RAA05736 for caml-red; Sat, 25 Nov 2000 17:04:43 +0100 (MET) Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id QAA17361 for ; Sat, 25 Nov 2000 16:54:16 +0100 (MET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id eAPFsDj13052; Sat, 25 Nov 2000 16:54:13 +0100 (MET) Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA08379; Sat, 25 Nov 2000 16:54:13 +0100 (MET) From: Pierre Weis Message-Id: <200011251554.QAA08379@pauillac.inria.fr> Subject: Re: Typing the result of a function In-Reply-To: from Mattias Waldau at "Nov 23, 100 11:25:32 pm" To: mattias.waldau@abc.se (Mattias Waldau) Date: Sat, 25 Nov 2000 16:54:13 +0100 (MET) Cc: caml-list@inria.fr X-Mailer: ELM [version 2.4ME+ PL28 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis@pauillac.inria.fr > I know how to type the arguments, and I like to do it, since I will get the > compile errors directly, not first when I use the function. > > Thus, I typical write a function like > > (* return the column called name *) > let find (columns:columns) (name:column_type) = > List.find ( fun column -> column.data_type = name ) columns.columns > > which has typing > > val find : columns -> column_type -> column = > > If I use the interactive environment, I see that I get the expected result > column. I needed to see this, since this is my first use of List.find, and I > wanted to be sure that it returned the column. > > However, I would have liked to say this already in the definition of find, > that the result of my function find should be a column. How is this done? > > /mattias > > P.s. I like to type, since I think it is belongs to the documentation of the > code. I would suggest to let those types in the module interfaces where you should already have written the documentation of the code then ... > P.P.s. How to type arguments is not very well described in the > documentation. I found one example, twice. Type constraints are simply introduced by a : symbol and should normally be enclosed into parens (as in your code above). However, you can use a simpler form to constraint the results of functions, just write the constraints before the = sign: let find (columns:columns) (name:column_type) : column = List.find ( fun column -> column.data_type = name ) columns.columns Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/