Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Why must types be always defined at the top level?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Richard Jones <rich@a...>
Subject: [Caml-list] Why must types be always defined at the top level?
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