Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* forward function definitions
@ 1999-06-09 18:38 Junbiao Zhang
  1999-06-14 10:55 ` Sven LUTHER
  0 siblings, 1 reply; 7+ messages in thread
From: Junbiao Zhang @ 1999-06-09 18:38 UTC (permalink / raw)
  To: caml-list

Hi,

	We're developing a fairly large system with multiple function
definitions. It's inevitable that some functions may call other functions
defined later(may not even be recursive calls as in the case of
Sys.Signal_handle). My question is: how do I solve such a problem which is
extremely trival in other languages?  Thanks. 

--Junbiao

My current solution is really ugly: 

let a_forward_ref = 
  (ref (fun _ -> raise (Failure "undefined") : int -> unit));;


let register_signals () = 
  .....
  Sys.signal Sys.sigalrm (Sys.Signal_handle !a_forward_ref)
  .....

.........

let a sigid = 
  ......

let assign_a_forward_ref () = 
  a_forward_ref := ref a	

And im main()(may be in another file):

  ......
  assign_a_forward_ref ()
  ......


I must be missing something or this language is:)




^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: forward function definitions
@ 1999-06-14 10:34 Toby Moth
  0 siblings, 0 replies; 7+ messages in thread
From: Toby Moth @ 1999-06-14 10:34 UTC (permalink / raw)
  To: caml-list, junzhang


> From Pierre.Weis@inria.fr Sat Jun 12 00:13:57 1999
> Date: Wed, 9 Jun 1999 14:38:22 -0400 (EDT)
> From: Junbiao Zhang <junzhang@ccrl.nj.nec.com>
> X-Sender: junzhang@flame
> To: caml-list@inria.fr
> Subject: forward function definitions
> MIME-Version: 1.0
> Sender: Pierre.Weis@inria.fr
> 
> Hi,
> 
> 	We're developing a fairly large system with multiple function
> definitions. It's inevitable that some functions may call other functions
> defined later(may not even be recursive calls as in the case of
> Sys.Signal_handle). My question is: how do I solve such a problem which is
> extremely trival in other languages?  Thanks. 
> 

Can't you just wrap up your deferred functions inside a mutable array or
an object ?

e.g. 

type 'a defer = { mutable exec : 'a }

let defer_fun = { exec = fun i -> print_int i }

let eval = defer_fun.exec;;

eval 3;;
defer_fun.exec <- fun i -> print_int ( i * 2 );;
eval 3;;

etc.
or

class defer = 
  object
    val mutable exec = fun i -> print_int i
    method update new_fun = exec <- new_fun
    method exec = exec
  end

....

I picked types here that match the types used in your example.

t




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~1999-06-17 15:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-06-09 18:38 forward function definitions Junbiao Zhang
1999-06-14 10:55 ` Sven LUTHER
1999-06-14 16:17   ` Pierre Weis
1999-06-16 13:03     ` luther sven
1999-06-16 20:55       ` Pierre Weis
1999-06-17  8:46         ` Sven LUTHER
1999-06-14 10:34 Toby Moth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox