English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Missing something in getting C and Ocaml to Work Together
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2000-11-25 (16:04)
From: Pierre Weis <Pierre.Weis@i...>
Subject: Re: Typing the result of a function
> 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 = <fun>
> 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/