Version française
Home     About     Download     Resources     Contact us    
Browse thread
Most recent CVS-branch?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Pouillard <nicolas.pouillard@g...>
Subject: Re: [Caml-list] Camlp4: ctyp antiquotation and polymorphic type question
Excerpts from Massimiliano Brocchini's message of Thu Jul 26 14:46:24 +0200 2007:
> Hi,
> 
> I have a type expression stored in a string (obtained by cmigrep) and I
> need to attach it to a polymorphic method definition, but I can't obtain
> what I expected... I tried two solutions but I miserably failed:

The antiquotation $anti:...$ is not made to be used by the end-user. If you
have a type expression as a string, then you have to parse it. You can do that
by making an OCaml parser by appling some functors and then be able to parse
it and inject it correctly.

However for quantified variables, I suggest you to switch to a string list and don't try to treat them at once.

(* not tested, but made for camlp4orf *)
open Camlp4.PreCast;;
module Caml =
  Camlp4OCamlParser.Make
    (Camlp4OCamlRevisedParser.Make
      (Camlp4.OCamlInitSyntax.Make(Ast)(Gram)(Quotation)));;

let function_type = "'a -> 'b" in
let quant = ["'a"; "'b"] in
let parse_ctyp = Gram.parse_string Caml.ctyp (Loc.mk "<string>") in
let quant =
  List.fold_right
    (fun t acc -> <:ctyp< $parse_ctyp t$ $acc$ >>) quant <:ctyp<>> in
let function_type = parse_ctyp function_type in
<:class_str_item< method $x$ : ! $list:quant$ . $function_type$ = $expression$ >>

HTH

-- 
Nicolas Pouillard aka Ertai