How to inject name in metaquot?

Is it possible to inject a string to be used as a (module) type name in metaquot? For example:

open Ppxlib

let some_expand_func ~loc ~path:_ mtd =
  let name : string = f(mtd) in (* Here `f` is an arbitrary function that builds the name of the output module *)
  [
    [%stri
      (* How to use `name` as the module name here? *)
      module $name = struct
        let msg = "foo"
      end];
  ]

I’m aware that I can build the node with a dummy name and then replace it. But I would like to know if there’s a cleaner metaquot-based solution that doesn’t involve anything extra (Ast_builder, etc.).

You can’t just inject a string, but if you build a value of type structure as name you should be able to use [%%s name] to unquote the item, no need to construct the node with a dummy name then.

Edit: You can see the unquoting operators explained in this part of the source code.

I’m sorry, could you elaborate a bit more?

This is obviously a syntax error, but I’m not sure if this is what you had in mind.

  (...)
  [ [%stri module [%%s name] = struct end] ]

Okay, I was a bit premature in declaring that this is easily possible, since name is not a structure_item but rather the pmb_name of a module_binding and I don’t think there is a way to construct such a thing directly using metaquot.

What I would do is to build the struct end part using metaquot and then pass it to the Pmod_structure constructor to get a value of module_expr_desc type, and then use that to build a module_expr and use that to build a module_binding. Yes, that’s a bit convoluted and I wished metaquot would allow to construct some more fine-grained tree types.