Version française
Home     About     Download     Resources     Contact us    
Browse thread
Estimating the size of the ocaml community
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Basile STARYNKEVITCH <basile@s...>
Subject: Re: Why can't types and exceptions be nested (was: Re: [Caml-list] Estimating the size of the ocaml community)
Le Sat, Feb 05, 2005 at 04:37:32PM +0000, Richard Jones écrivait/wrote:
> On Sat, Feb 05, 2005 at 02:14:18PM +0100, Oliver Bandel wrote:
> > ...well....
> > 
> > type sometimes_t = Integer of int | One_symbol | Another_symbol
> 
> Another annoyance of OCaml is that 'type' definitions (and exception
> defns for that matter) can't be nested.  I'm a big big fan of nested
> functions, and sometimes I want to return a type which is only used
> briefly between two nested functions. 

You can use the let module syntax for that, eg (the example is silly,
but it works)

# let pubf x = 
   let module Internal = struct
     type brief_t = { a: string; b: int }
     let fun1 x = {a= string_of_int x; b= x+2 }
     let rec fun2 i = if i<=0 then [] else fun1 i :: (fun2 (i-1))
     let f x = List.map (fun {a=s; b=n} -> s,n) (fun2 x)
   end 
  in Internal.f x;;

# pubf 4;;
- : (string * int) list = [("4", 6); ("3", 5); ("2", 4); ("1", 3)]


Regards.
-- 
Basile STARYNKEVITCH         http://starynkevitch.net/Basile/ 
email: basile<at>starynkevitch<dot>net 
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France