Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2004-04-06 (17:39)
From: brogoff@s...
Subject: Re: [Caml-list] Function forward declaration?
On Tue, 6 Apr 2004, [iso-8859-1] Correnson Loïc wrote:
> b) You can also define *real* forward function by using references (already
> suggested)
>     However, you may loose polymorphism, if any, due to (_a ->_b) ref types.

You may sidestep this issue by rolling your own refs using the explicit
polymorphism on record fields, sketched below:

type fwd_ref = { mutable f : 'a 'b . 'a -> 'b }

module Module1 = struct
  let forward_g = { f = (fun x -> failwith "forward_g") }

  let f = forward_g.f

module Module2 = struct
  let g = Module1.f

  let _ = Module1.forward_g.f <- g

and of course you can make it as pretty (yeah, I know, dressing up a pig)
as refs if you wish.

There was a discussion of this a looooong time ago, in the context of supporting
polymorphic recursion in the language, and I believe it was mentioned that some
experimental Caml compiler had a forward declaration capability. It seems like a
good idea, and I wonder why it hasn't made it into the language, as this is a
FAQ and a language blemish.

-- Brian

To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: