Giter Club home page Giter Club logo

ppx_match_literals's Introduction

ppx_match_literals

ppx_match_literals is an OCaml extension written using ppxlib that allows for variables to be placed in match patterns and have them be interpreted as their literal value. Consider the following simple example usage:

match%literals var_1 with
| (_, [%lit var_2]) -> expr

How to use as literals

To specify that a match expression will have patterns that require this extension, create the match statement using the extension %literals with infix syntax; ie, use match%literals instead of match when going to use literal variables in the cases. Inside a match%literals expression, literal variables can be specified in a case pattern using the %lit extension with the variable name as the parameter. Usage would look like [%lit var_name_here].

How it works

This extension traverses the patterns of all match%literals expressions and replaces the extension nodes representing [%lit id] patterns with wildcard patterns. An extra when guard is appended for every wildcard that needs to be matched to a variable. To avoid namespace collision, wildcards are given the name ppx_match_literals_lit_* where "*" is replace with the name of the variable to match.

Currently supports recursing into the following patterns

  • tuple
  • construct
  • record
  • alias
  • or

Example usage

Before desugaring, using extension

let hello = "hello" in
let hi = "hi" in
match%literals ("hello", "abc", "hi") with
| ([%lit hello], "abc", _) when boolean_1 -> 1
| (_, "123", [%lit hi]) -> 2
| ([%lit hello], "abc", [%lit hi]) -> 3
| _ -> 4

After desugaring

let hello = "hello" in
let hi = "hi" in
match ("hello", "abc", "hi") with
| (ppx_match_literals_lit_hello, "abc", _) when 
    boolean_1 && ppx_match_literals_lit_hello = hello -> 1
| (_, "123", ppx_match_literals_lit_hi) when ppx_match_literals_lit_hi = hi -> 2
| (ppx_match_literals_lit_hello, "abc", ppx_match_literals_lit_hi) when 
    ppx_match_literals_lit_hello = hello && ppx_match_literals_lit_hi = hi-> 3
| _ -> 4

More complicated usage can be found in example.ml.

Acknowledgement

Large thanks to jacketsj for the initial idea and directional advice for implementation.

ppx_match_literals's People

Contributors

ryco117 avatar

Watchers

 avatar  avatar

ppx_match_literals's Issues

Investigate why array patterns don't match

There is currently commented-out code for matching array patterns, however, it seems to compile the elements in the array before the match expression, making it impossible for the match%literals to replace the [%lit ..] extension before it is compiled (with an error).

A possible fix might be to register an extension for [%lit ..] that simply maps them to themselves, possibly avoiding the unknown extension error.

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.