package ppx_import

  1. Overview
  2. Docs
A syntax extension for importing declarations from interface files

Install

Dune Dependency

Authors

Maintainers

Sources

ppx_import-1.10.0.tbz
sha256=300f2c7f417b0a1d702432fc13ce3bd2e90ac7b2a2796ca35899c942ca81556f
sha512=835d5abff0f1eba28313f5925beaeb8c27a4458c91cf395fdd28b984b0745aad8725b4a8a921517ad09ed567ff8586f48a4afa4127eab6c2f773efc5d50c40fb

Description

Tags

syntax

Published: 14 Jun 2022

README

[%%import]

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.

Dependencies (4)

  1. ppxlib >= "0.26.0" & < "0.32.1~5.2preview"
  2. dune >= "1.11.0"
  3. ocaml >= "4.10.0" & < "5.2"
  4. ocaml >= "4.05.0" & < "4.10.0"

Dev Dependencies (3)

  1. ppx_deriving with-test & >= "4.2.1"
  2. ounit with-test
  3. ppx_sexp_conv with-test & >= "v0.13.0"

Conflicts

None