Browse thread
[Caml-list] Function forward declaration?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| 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
end
module Module2 = struct
let g = Module1.f
let _ = Module1.forward_g.f <- g
end
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 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