# Formatter string provided by a function. How can I do that?

**URL:** <https://discuss.ocaml.org/t/formatter-string-provided-by-a-function-how-can-i-do-that/14759>\
**Category:** Learning\
**Created:** [June 7, 2024, 6:39pm UTC](https://discuss.ocaml.org/t/formatter-string-provided-by-a-function-how-can-i-do-that/14759 "2024-06-07T18:39:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![nickm](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nickm](https://discuss.ocaml.org/u/nickm)\
**Post date:** [June 7, 2024, 6:39pm UTC](https://discuss.ocaml.org/t/formatter-string-provided-by-a-function-how-can-i-do-that/14759/1 "2024-06-07T18:39:28Z")

</div>

Hi there!  
I’m stuck again.

Let’s say I want to print some text, formatted by a formatting string. Simple!

```auto
      bprintf buff "%s<%s/>" (String.make (indent * level) ' ') element.tagStr;

```

Now, the formatter string gets “more complex” and is provided by the function getFmt

```auto
type tagVariant = XvTagEmpty | XvTagOpen | XvTagClose | XvNoTag (* <x/> | <x> | </x> | no tag found *)

let rec xmlFormat ?(indent = 2) ?(level = 0) element =
  let getFmt tagType =
    match tagType with 
    | XvTagEmpty -> "%s<%s/>" 
    | XvTagOpen -> "%s<%s>"
    | XvTagClose -> "%s<%s/>"
    | XvNoTag -> "%s error %s"
  in

  let buff = Buffer.create 100 in
      bprintf buff (getFmt XvTagEmpty) (String.make (indent * level) ' ') element.tagStr;

```

The formatter string always has the same pattern of requiring two strings, as in the initial example.  
I’m sorry, but I don’t understand how to get that working. It seems that Scanf.format\_from\_string is part of the solution. I do understand why OCaml is so strict about this subject. It needs to analyze the formatter string to avoid any type mismatches.

Any help/hints are welcome.

---

<div class="post-metadata">

**Author:** ![nojb](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/nojb/32/519_2.png) [@nojb](https://discuss.ocaml.org/u/nojb)\
**Post date:** [June 7, 2024, 6:51pm UTC](https://discuss.ocaml.org/t/formatter-string-provided-by-a-function-how-can-i-do-that/14759/2 "2024-06-07T18:51:52Z")

</div>

> [@nickm](#):
>
> Any help/hints are welcome.

The compiler has no way of knowing that the return type of `getFmt` is a formatting string instead of a plain string (since both use the same syntax). You need to help the compiler by annotating the function:

```auto
let getFmt tagType : _ format =

```

Cheers,  
Nicolas
