1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00

Add Dynamically Create A Printf String Format as a reasonml til

This commit is contained in:
jbranchaud
2018-07-01 16:39:59 -05:00
parent e591de5b6e
commit 26d060a4f7
2 changed files with 36 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
# Dynamically Create A Printf String Format
Formatting a string with `Printf` requires defining a format for that
string.
```reason
let str = Printf.sprintf("%6s", "dope");
/* str => " dope" */
```
The _format_ is the first argument. At compile-time it is interpreted as a
`format6` type value.
So, what if you want a dynamically created _format_ value? Simply
concatenating some strings together won't do it because then the type will
be `string` and that's not going to compile.
The [`Scanf.format_from_string`](https://reasonml.github.io/api/Scanf.html)
function can help.
```reason
let some_num = 6;
let format_str = "%" ++ string_of_int(some_num) ++ "s";
let format = Scanf.format_from_string(format_str, "%s");
let str = Printf.sprintf(format, "dope");
/* str => " dope" */
```
We can convert our string that has the appearance of a format into an actual
`format6` type. To do this, we have to tell `format_from_string` what types
each of the formats is going to have -- hence the second argument `%s`.
[source](https://twitter.com/rickyvetter/status/1013476235253436417)