Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Function forward declaration?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Remi Vanicat <remi.vanicat@l...>
Subject: Re: [Caml-list] Function forward declaration?
Timo.Tapiola@tietoenator.com writes:

> Hi,
>
> could someone tell me if there is some way to forward declare functions in
> OCaml?

no, but you can :

use the "let rec ... and .. and ... " construct :

let rec simpl1 x y = simpl2 y x
and simpl2 x y = simpl1 y x

or use local function :

let rec simpl1 x y =
  let simpl2 x y = simpl1 y x in
  simpl2 y x

You can even use both technique at the same time.

The third technique is less clean, and is rarely needed : it use
references :

let simpl1_ref = ref (fun x -> assert false)

let simpl2 x y = !simpl1_ref y x
let simpl1 x y = simpl2 x y
let _ = simpl1_ref := simpl1

both two first method cover most of the case, the third one is needed
only in very long code, or in recurrence between compilation unit.

[...]

-- 
Rémi Vanicat

-------------------
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