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: Issac Trotts <ijtrotts@u...>
Subject: Re: [Caml-list] Function forward declaration?
On Tue, Apr 06, 2004 at 02:37:11PM +0200, Remi Vanicat wrote:
> 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.

This cleaner alternative should cover some of those cases:

    let simpl2 f x y = f y x
    let rec simpl1 x y = simpl2 simpl1 x y

-- 
Issac Trotts
http://mallorn.ucdavis.edu/~ijtrotts
(w) 530-757-8789

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