package ppx_optional

All checked builds pass
  1. Overview
  2. Docs

Description

A ppx rewriter that rewrites simple match statements with an if then else expression.

Added to opam-repository:

README

ppx_optional - match statements for zero-alloc options

A ppx rewriter that rewrites simple match statements with an if then else expression.

Syntax

ppx_optional rewrites the extension match%optional in expressions. It requires that a module Optional_syntax is in scope, and that it has is_none and unsafe_value values.

For instance:

match%optional e with
| None -> none_expression
| Some x -> some_expression

becomes:

if Optional_syntax.is_none e then begin
   none_expression
end else begin
   let x = Optional_syntax.unsafe_value e in
   some_expression
end

Another example with multiple values and inline getting Optional_syntax in scope:

match%optional.Price_float.Option a, b with
| None, None -> expression_a
| Some a, None -> expression_b
| None, Some b -> expression_c
| Some a, Some b -> expression_d

If any of the expressions to be matched are unboxed, you will need to use %optional_u instead. This works just as well as %optional (and could be used everywhere, safely), but gives worse error messages in the case of an incomplete pattern match. If you observe other differences between %optional and %optional_u (for example, behavior around merlin queries), please report bugs to maintainers.

Usage

This is normally used to safely access an optional value while avoiding allocation for immediate values (e.g. Immediate.{Char,Bool,Int}.Option, Fixed.Option, Price.Fixed.Option, etc...).

Dependencies (5)

  1. ppxlib >= "0.28.0"
  2. dune >= "3.11.0"
  3. ppxlib_jane >= "v0.17" & < "v0.18~"
  4. base >= "v0.17" & < "v0.18~"
  5. ocaml >= "5.1.0"

Dev Dependencies

None

Optional Dependencies

None

Used by (3)

  1. float_option
  2. ooh
  3. ppx_jane >= "v0.17.0"

Conflicts

None

Build status

Results of building ppx_optional with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
v0.17.0 (this version) n/a n/a good good good
v0.16.0 n/a good good good good
v0.15.0 good good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure