caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Why must types be always defined at the top level?
@ 2004-06-22 22:41 Richard Jones
  2004-06-22 22:53 ` Markus Mottl
  0 siblings, 1 reply; 37+ messages in thread
From: Richard Jones @ 2004-06-22 22:41 UTC (permalink / raw)
  To: caml-list

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


^ permalink raw reply	[flat|nested] 37+ messages in thread

end of thread, other threads:[~2004-07-10 10:56 UTC | newest]

Thread overview: 37+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-06-22 22:41 [Caml-list] Why must types be always defined at the top level? Richard Jones
2004-06-22 22:53 ` Markus Mottl
2004-06-22 23:32   ` skaller
2004-06-23 12:01     ` Andreas Rossberg
2004-06-23 14:45       ` skaller
2004-06-23 16:28         ` Andreas Rossberg
2004-06-23 20:21           ` skaller
2004-06-23 20:52             ` skaller
2004-06-24 14:27               ` John Hughes
2004-06-24 16:47                 ` Andreas Rossberg
2004-06-24 17:30                   ` Markus Mottl
2004-06-24 17:45                 ` Xavier Leroy
2004-06-24 19:46                   ` John Hughes
2004-06-24 19:56                     ` David Brown
2004-06-24 19:57                     ` William D. Neumann
2004-06-24 20:13                       ` Olivier Andrieu
2004-06-24 23:26                     ` Brian Hurt
2004-06-25 10:20                     ` skaller
2004-06-25 11:07                       ` Basile Starynkevitch [local]
2004-06-25 12:30                         ` skaller
2004-06-25 14:38                           ` [Caml-list] Thread and kernel 2.6 pb still there in CVS Christophe Raffalli
2004-06-25 16:08                             ` [Caml-list] " Marco Maggesi
2004-06-25 16:32                               ` Markus Mottl
2004-06-28 15:08                             ` [Caml-list] " Xavier Leroy
2004-06-28 18:50                               ` Benjamin Geer
2004-06-29  2:26                               ` Christophe Raffalli
     [not found]                                 ` <7AFB5F64-C944-11D8-975C-00039310CAE8@inria.fr>
     [not found]                                   ` <40E11621.3050709@univ-savoie.fr>
2004-07-05 15:14                                     ` Christophe Raffalli
2004-07-05 16:34                                       ` Xavier Leroy
2004-07-06  9:33                                         ` Alex Baretta
2004-07-08 13:51                                           ` Christophe Raffalli
2004-07-08 15:03                                             ` Xavier Leroy
2004-07-09 23:21                               ` Donald Wakefield
2004-07-10 10:56                                 ` Damien Doligez
2004-06-24 23:23                   ` [Caml-list] Why must types be always defined at the top level? Brian Hurt
     [not found]                     ` <Pine.LNX.4.44.0406241813370.4202-100000@localhost.localdom ain>
2004-06-26 23:08                       ` Dave Berry
2004-06-25  1:59                   ` Yaron Minsky
2004-06-24 23:08                 ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).