neojski / ppx_string

ppx extension for string interpolation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This extension provides a syntax for string interpolation. Here is an example of all the features of ppx_string:

let script_remotely (user : string) (host : string) (port : int) (script : string) =
  [%string "ssh %{user}@%{host} -p %{port#Int} %{Sys.quote script}"]

The above expression is equivalent to:

let script_remotely (user : string) (host : string) (port : int) (script : string) =
  String.concat ""
    [ "ssh "
    ; user
    ; "@"
    ; host
    ; " -p "
    ; Int.to_string port
    ; " "
    ; Sys.quote script
    ]

Compared to `Printf.sprintf`:

let script_remotely (user : string) (host : string) (port : int) (script : string) =
  sprintf "ssh %s@%s -p %d %s" user host port (Sys.quote script)

having the values inline instead of after the format string can make it easier to understand the resulting string, and avoids the potential mistake of passing arguments in the wrong order. This is truer the more format arguments there are. On the other hand, some things are much easier with printf: pad numbers with zero or spaces, pad strings with spaces, display floats in a specific formats, etc.

Compared to manually writing something like `String.concat` version above, ppx_string is shorter and can oftentimes be less error-prone (it’s really easy to forget whitespace after `ssh` or around `-p` in the explicit `String.concat` version).

To include a literal %, say

[%string {|%{"%"}|}]

About

ppx extension for string interpolation

License:MIT License


Languages

Language:OCaml 96.2%Language:Makefile 3.8%