From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by yquem.inria.fr (Postfix) with ESMTP id 75B57BBAF for ; Wed, 18 Nov 2009 15:33:48 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AkQBAL2UA0uB8ApHmWdsb2JhbACcAgEBAQEBCAsKBxO6AIQ7BA X-IronPort-AV: E=Sophos;i="4.44,765,1249250400"; d="scan'208";a="40345465" Received: from mail-forward2.uio.no ([129.240.10.71]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/DHE-RSA-AES256-SHA; 18 Nov 2009 15:33:48 +0100 Received: from exim by mail-out2.uio.no with local-bsmtp (Exim 4.69) (envelope-from ) id 1NAlbP-0005zl-BI for caml-list@inria.fr; Wed, 18 Nov 2009 15:33:47 +0100 Received: from mail-mx2.uio.no ([129.240.10.30]) by mail-out2.uio.no with esmtp (Exim 4.69) (envelope-from ) id 1NAlbP-0005zi-AG for caml-list@inria.fr; Wed, 18 Nov 2009 15:33:47 +0100 Received: from [128.39.37.254] (helo=[192.168.100.135]) by mail-mx2.uio.no with esmtpsa (TLSv1:AES256-SHA:256) user hans (Exim 4.69) (envelope-from ) id 1NAlbO-0006dd-Vh for caml-list@inria.fr; Wed, 18 Nov 2009 15:33:47 +0100 Message-ID: <4B0405CA.80704@simula.no> Date: Wed, 18 Nov 2009 15:33:46 +0100 From: Hans Ole Rafaelsen User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 To: caml-list@inria.fr Subject: Polymorphic function in reference cell Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-UiO-Ratelimit-Test: rcpts/h 1 msgs/h 1 sum rcpts/h 2 sum msgs/h 2 total rcpts 734 max rcpts/h 10 ratelimit 0 X-UiO-Spam-info: not spam, SpamAssassin (score=-5.0, required=5.0, autolearn=disabled, UIO_MAIL_IS_INTERNAL=-5, uiobl=NO, uiouri=NO) X-UiO-Scanned: DD523C3BE9CA7FEF1A4478C1BE76D110ED0ABA29 X-UiO-SPAM-Test: remote_host: 128.39.37.254 spam_score: -49 maxlevel 80 minaction 2 bait 0 mail/h: 11 total 7293 max/h 49 blacklist 0 greylist 0 ratelimit 0 X-Spam: no; 0.00; rafaelsen:01 foo:01 foo:01 struct:01 1.0:98 1.0:98 polymorphic:01 polymorphic:01 functions:01 functions:01 undefined:01 undefined:01 defined:02 module:03 module:03 Hi, I have a module that have several functions that take a polymorphic function as part of their arguments (Foo.f1 and Foo.f2 in the example). module Foo = struct let f1 f = f 1 let f2 f = f 1.0 let f1_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) )) let set_f1_ref f = f1_ref := f let f2_ref = (ref (fun _ -> raise (Failure "undefined") : ('a -> 'a) )) let set_f2_ref f = f2_ref := f let use_f1 () = !f1_ref 1 let use_f2 () = !f2_ref 1.0 let internal_f v = v let internal_f1 () = internal_f 1 let internal_f2 () = internal_f 1.0 end let f v = v let a = Foo.f1 f let b = Foo.f2 f let () = Foo.set_f1_ref f let () = Foo.set_f2_ref f let c = Foo.use_f1 () let d = Foo.use_f2 () In my code I don't want to pass this function around to to all functions where Foo.f1 is called. For this reason I would like to store this function that Foo.f1 needs within Foo. (The function is created outside the module and bound with values generated outside the module.) However you can not store polymorphic functions in references. So I have to make one reference for each concrete type it is used for. What I really would like is use it as if it was defined within Foo, like internal_f and used like the internal_f1 and internal_f2. Is it possible to only store one reference to this kind of function within a module, or do I really have to write a reference for each usage of the function? If I have to create a reference for each case, do anyone have some trick to avoid code repetition? Regards, Hans Ole