From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by sympa.inria.fr (Postfix) with ESMTPS id 01994800B6 for ; Wed, 4 Jan 2017 14:08:59 +0100 (CET) X-IronPort-AV: E=Sophos;i="5.33,459,1477954800"; d="scan'208";a="206928994" Received: from dt-64014.paris.inria.fr (HELO [128.93.64.14]) ([128.93.64.14]) by mail3-relais-sop.national.inria.fr with ESMTP/TLS/DHE-RSA-AES128-SHA; 04 Jan 2017 14:08:59 +0100 To: whitequark@whitequark.org, caml users From: =?UTF-8?Q?Fran=c3=a7ois_Pottier?= Message-ID: <9576980c-9aad-4af2-e512-dc9c42743cf2@inria.fr> Date: Wed, 4 Jan 2017 14:08:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: [Caml-list] ppx_deriving question: deferring code generation? Hello all, I am currently in the process of writing a ppx_deriving plugin, called "visitors". Overall, this has been a pleasant experience; a few hundred lines of code have been sufficient to obtain nontrivial results. In normal use, the user writes something like this: type foo = Bar | Baz [@@deriving visitors] and some generated code is inserted just after the definition of the type foo. However, I have reached a situation where the generated code cannot be placed just after the type definition. That is, I need to allow user-written code to appear after the type definition and before the generated code. For instance, this user-written code could be a declaration of a global variable "x", whose type is "foo ref", and which the generated code uses. The declaration of "x" must appear after the definition of the type "foo", because the type of "x" mentions "foo". And the declaration of "x" must appear before the generated code, because the generated code (intentionally) refers to "x". I am imagining that perhaps the user could write something like this: type foo = Bar | Baz [@@deriving visitors { delayed = true } let x : foo option ref = ref None [@@visitors] The effect of the flag { delayed = true } would be to store the generated code somewhere in memory (instead of emitting right away), and the effect of the floating attribute [@@visitors] would be to fetch that code from memory and emit it. To me, this seems somewhat ugly, but workable. Does ppx_deriving offer a better approach? Does anyone have a better suggestion? Comments are appreciated. Best regards, -- François Pottier francois.pottier@inria.fr http://gallium.inria.fr/~fpottier/