Skip to content

camlp5/pa_ppx_static

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pa_ppx_static version 0.01: "static blocks" for OCaml

This package provides two PPX extensions:

[%static ...]

This can be used anywhere an expression can be used. The payload must be an expression, and that expression will be moved to the top of the file (or compilation unit) where it will be assigned a fresh name; that name will be used in place of the expression, so that the expression will be evaluated only once. Viz.

let f s =
  let re = [%static Pcre2.regexp "(a)(b)(c))"] in
  Pcre2.exec ~rex s
[%%static_preamble ...]

This can only be used at the first code in a file (not in the toplevel) (e.g. at top of file, but possibly after comments) and provides statements that will precede all the expressions in static blocks. That is to say (again, only in files), the preamble

[%%static_preamble open Pcre2]

could then allow the example above to become

let f s =
  let re = [%static regexp "(a)(b)(c))"] in
  Pcre2.exec ~rex s

NOTE that the Pcre2.exec hasn’t changed: we didn’t open Pcre2 for the entire file, only for the static blocks.

Installation and Invocation

To install this package via opam:

opam install pa_ppx_static

to build and install, once pa_ppx is installed, make all test will do the rest.

Usage via the OCaml compiler

To use this PPX rewriter via the OCaml batch compiler, put your static declarations

In the OCaml toplevel

# #use "topfind.camlp5";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

- : unit = ()
Additional Camlp5 directives:
  #camlp5o;;                to load camlp5 (standard syntax)
  #camlp5r;;                to load camlp5 (revised syntax)

- : unit = ()
# #camlp5o ;;
: added to search path
: added to search path
: loaded
: added to search path
: loaded
# #require "camlp5.pr_o";;
# #require "pa_ppx_static" ;;

Examples

# let x = [%static 2] ;;
val __static_0__ : int Pa_ppx_static_runtime.Static.t = <abstr>
val x : int = 2
# [%static 1] ;;
- : int = 1