From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id AAA10128; Wed, 23 Jun 2004 00:41:27 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 AAA02193 for ; Wed, 23 Jun 2004 00:41:26 +0200 (MET DST) Received: from aomori.annexia.org (annexia.force9.co.uk [212.56.101.183]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i5MMfPSH011790 for ; Wed, 23 Jun 2004 00:41:26 +0200 Received: from rich by aomori.annexia.org with local (Exim 3.36 #1 (Debian)) id 1BctxF-000798-00 for ; Tue, 22 Jun 2004 23:41:25 +0100 Date: Tue, 22 Jun 2004 23:41:25 +0100 To: caml-list@inria.fr Subject: [Caml-list] Why must types be always defined at the top level? Message-ID: <20040622224125.GA24785@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.5.1+cvs20040105i From: Richard Jones X-Miltered: at concorde with ID 40D8B595.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; subroutine:01 char:01 catered:99 type-safe:01 ltd:98 caml:01 caml:01 rec:01 match:02 match:02 nested:02 nested:02 module:03 module:03 abstract:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk The subject says it all really. Why do types need to be defined at the top level of a module? Why isn't it possible to define them in some nested scope? A practical example of where this is very useful is defining a subroutine which uses a state machine. I want to define the state machine type close to the (nested) function which uses the type, not somewhere near the top of the file. Actual example: type state_t = StartField | InUnquotedField | InQuotedField | InQuotedFieldAfterQuote let load_rows f chan = (* ... *) let state = ref StartField in (* Current state. *) (* ... *) let rec loop () = let c = input_char chan in if c != '\r' then ( match !state with StartField -> (* ... *) | InUnquotedField -> (* Reading chars to end of field. *) (* ... *) | InQuotedField -> (* Reading chars to end of field. *) (* ... *) | InQuotedFieldAfterQuote -> ); (* end of match *) loop () (* ... *) In this example, it would be much more convenient to define the start_t type within the load_rows function, since nothing outside this function ever uses this state. I realise of course that types might be returned from functions, but surely the same thing happens when one module returns an abstract type to another function? Either this could be catered for in the same way, or could be prevented. Rich. -- Richard Jones. http://www.annexia.org/ http://www.j-london.com/ Merjis Ltd. http://www.merjis.com/ - improving website return on investment MOD_CAML lets you run type-safe Objective CAML programs inside the Apache webserver. http://www.merjis.com/developers/mod_caml/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners