package ppx_import

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

Install

Dune Dependency

Authors

Maintainers

Sources

bd627d5afee597589761d6fee30359300b5e1d80.tar.gz
sha256=983445f1b5a06f92104091b9e5bb29dbb6bda2d6e11674dd0e78380ffa7f4a51
md5=0d04de821f75be32427bebcedae79e8d

Description

A syntax extension for importing declarations from interface files

Tags

syntax

Published: 11 Nov 2018

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.

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 (5)

  1. ocaml-migrate-parsetree >= "1.1.0" & < "2.0.0"
  2. ppx_tools_versioned >= "5.2.1"
  3. ppxlib >= "0.3.1" & < "0.9.0"
  4. dune >= "1.2.0"
  5. ocaml >= "4.04.2" & < "4.08.0"

Dev Dependencies (2)

  1. ppx_deriving with-test & >= "4.2.1"
  2. ounit with-test

Used by (14)

  1. coq-serapi < "8.20.0+0.20.0"
  2. elpi >= "1.10.0" & < "1.11.0"
  3. frama-c >= "26.0~beta" & < "29.0~beta"
  4. goblint < "1.1.1"
  5. modelica_ml >= "0.2.0"
  6. octez-l2-libs
  7. ppx_deriving_cmdliner < "0.6.0"
  8. ppx_deriving_morphism
  9. satyrographos >= "0.0.2.13"
  10. systemverilog < "0.0.2"
  11. tezos-scoru-wasm-helpers
  12. uwt < "0.3.3"
  13. vscoq-language-server
  14. wikitext

Conflicts

None

OCaml

Innovation. Community. Security.