GithubHelp home page GithubHelp logo

smolkaj / ppx_import Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ocaml-ppx/ppx_import

0.0 3.0 0.0 43 KB

Less redundancy in type declarations and signatures

License: MIT License

Makefile 3.94% OCaml 96.06%

ppx_import's Introduction

[%%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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.