ppx_import

A syntax extension for importing declarations from interface files
README

import is a syntax extension that allows to pull in types or signatures from other compiled interface files.

Sponsored by Evil Martians.

Installation

import can be installed via OPAM:

$ opam install ppx_import

Usage

In order to use import, require the package ppx_import.

Using ppx_import from Dune

To use ppx_import from Dune you should use the staged_pps field to declare the preprocessing specification. Example:

(library
  (name foo)
  (preprocess (staged_pps ppx_import ppx_deriving.show))

Syntax

Single declarations

For example:

# type loc = [%import: Location.t];;
type loc = Location.t = { loc_start : Lexing.position; loc_end : Lexing.position; loc_ghost : bool; }
# module type Hashable = [%import: (module Hashtbl.HashedType)];;
module type Hashable = sig type t val equal : t -> t -> bool val hash : t -> int end

It is also possible to import items from your own .mli file.

Combining with [@@deriving]

It's possible to combine import and deriving to derive functions for types that you do not own, e.g.:

type longident = [%import: Longident.t] [@@deriving show]
let () =
  print_endline (show_longident (Longident.parse "Foo.Bar.baz"))
(* Longident.Ldot (Longident.Ldot (Longident.Lident ("Foo"), "Bar"), "baz") *)

Note that you need to require import before any deriving plugins, as otherwise deriving will not be able to observe the complete type.

[@with] replacements

It is possible to syntactically replace a type with another while importing a definition. This can be used to import only a few types from a group, or to attach attributes to selected referenced types.

For example, this snippet imports a single type from Parsetree and specifies a custom pretty-printer for deriving show.

type package_type =
[%import: Parsetree.package_type
          [@with core_type    := Parsetree.core_type [@printer Pprintast.core_type];
                 Asttypes.loc := Asttypes.loc [@polyprinter fun pp fmt x -> pp fmt x.Asttypes.txt];
                 Longident.t  := Longident.t [@printer pp_longident]]]
[@@deriving show]

For module types, the replacements are specified using the standard with construct. However, the replacement is still syntactic.

More?

If you have a use case in mind that ppx_import does not cover (in particular, object-oriented features are not implemented), please open an issue.

License

import is distributed under the terms of MIT license.

Install
Published
26 Feb 2020
Sources
ppx_import-v1.7.1.tbz
sha256=9b48c45e60727e51b1a9c49d86e73bf4efe2ad19d81f85732ae67b7d96ebbe99
sha512=087a61d828a9de31c279d3810ae9db4e9e0b65d22975ba07bc26ad51578bce9475077d34631ad4d2602a783d1febd2bbf38cde042ea43c5bed630c6720028346
Dependencies
ppx_deriving
with-test & >= "4.2.1"
ounit
with-test
ocaml-migrate-parsetree
>= "1.2.0" & < "2.0.0"
ppxlib
>= "0.3.1"
dune
>= "1.2.0"
ocaml
>= "4.04.2"
Reverse Dependencies
elpi
>= "1.10.0" & < "1.11.0"
frama-c
>= "26.0~beta"
goblint
< "1.1.1"
modelica_ml
>= "0.2.0"
pa_ppx
< "0.07"
systemverilog
< "0.0.2"
uwt
< "0.3.3"