Coder Social home page Coder Social logo

ocp-ppx-namespace's Introduction

ocp-ppx-namespace

A ppx to simplify the usage of module aliases wrappers in OCaml, and allowing a form of selective open and selective include.

This ppx adds an extension point for structures (not signatures) to import multiple submodules from a given module. It is especially useful when using module aliases to simulate namespaces.

The syntax is simple:

(* myfile.ml *)
[%%import MyModule.(Mod1, Mod2, Mod3 => Mod3Alias)]

This simple piece of code will generate the following code:

module Namespace-Myfile-Mymodule-1 = struct
  module Mod1 = MyModule.Mod1
  module Mod2 = MyModule.Mod2
  module Mod3Alias = MyModule.Mod3
end
open Namespace-Myfile-MyModule-1

Of course, it is not a valid module identifier, to avoid clashes with other modules of the user. The 1 at the end of the module name is a unique stamp, to allow reimporting from an already used module.

It can be used in combination with ocp-nsgen to use external libraries as namespaces.

The following syntax is also available to import a unique module:

[%%import MyModule.M1]

Selective include

Using the directive include instead of import generates:

(* myfile.ml *)
[%%include MyModule.Mod1]
module Namespace-Myfile-Mymodule-1 = struct
  module Mod1 = MyModule.Mod1
end
include Namespace-Myfile-MyModule-1

Value import

Values can also be imported from a module:

[%%import MyModule.(my_function, my_value => v)]

Of courses, modules and values can be imported in the same import directive.

Types and module types import

Using the attribute [@type] on each imported element (or globally) results in importing a type with the qualified name instead of a value (and reciprocally with moduel types instead of module).

However, it will need to read the signature of the module to generate the correct manifest and kind for type declarations, i.e.:

(* myfile.ml *)
[%%import Pervasives.(option [@type])]

must be translated as:

module Namespace-Pervasives-Myfile-1 = struct
  type 'a option = 'a Pervasives.option = Some 'a | None
end
open Namespace-Pervasives-Myfile-1

Otherwise, if it simply abbreviated (without writing the complete definition), the constructors Some and None won't be in the scope of the current module. The cmi files are necessary.

Type extensions and exceptions

Not implemented yet.

Classes and class types

Not implemented yet.

Namespace declaration

The namespace declaration is also usable:

[%%namespace MyModule]

which is simply an open MyModule. It simply avoids dependencies with ocamldep and module aliases: it won't generate a dependency to MyModule in this specific case (useful when MyModule has been compiled with -no-alias-deps and is a wrapper to simulate a namespace).

TODO:

  • Allow imports in signature, which is not that relevant: the generated signature will contain this specific generated module which is unusable. Moreover, types can be prefixed with the correct module.
  • Installation

ocp-ppx-namespace's People

Watchers

Pierrick Couderc avatar

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.