GithubHelp home page GithubHelp logo

cryptosense / ppx_factory Goto Github PK

View Code? Open in Web Editor NEW
13.0 6.0 1.0 103 KB

OCaml preprocessor to derive factory methods and default values from type definitions

License: BSD 2-Clause "Simplified" License

Standard ML 0.47% OCaml 94.12% SourcePawn 5.41%
ocaml ppx factory testing

ppx_factory's People

Contributors

bbc2 avatar ian-barnes avatar leamingrad avatar nathanreb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Forkers

nathanreb

ppx_factory's Issues

Re-introduce errors when deriving factories from abstract types

As soon as #13 gets merged ppx_factory will silently ignore derivations from abstract types in order to be compatible with ppx_import.

This is because ppxlib doesn't expose the tool_name required to only adopt this behaviour when called from ocamldep.

Once this is available to the deriver, we should bring back the error in other contexts.

There's an existing issue about this in ppxlib: ocaml-ppx/ppxlib#61

Derive default from variant types

We need to be able to derive default values from variant types.
We should at least handle non parametrized variant types.

In the case of parametrized variant types we'd ideally like to be able to detect whether a generic default can be derived and do so or fail otherwise.

Ie in this example code

type ('a, 'b) t =
  | A of 'a
  | B of 'b
[@@deriving default]

type ('a, 'b) u =
  | A of 'a
  | B of 'b
  | C
[@@deriving default]

We should be able to derive the following default for u

let default = C

but fail on t

Compatibility with ppx_import

Atm [@@deriving factory] doesn't work with ppx_import.

Ppx-es are called twice, first with ocamldep to resolve dependencies of a module, including dependencies of the generated code, and then with the actual compiler.

Because ppx_import needs bits of information from .cmi files, when called with ocamldep it can't expand completely and thus only expands into an abstract type. For example the following code:

type t = [%import: A.t]

is expanded as:

type t = A.t

during the first phase and as:

type t = A.t = ...

during the second.

Because we don't derive factories for abstract types, using both ppx_import and ppx_factory will make the build fail during that first phase.

In order for it to work, we'd ideally need to have a special case when called from ocamldep so that abstract types are silently "skipped" and only trigger an error during that second phase.

See ocaml/dune#1861 for details.

Derive default for polymorphic variants

I'm not super familiar with the polymorphic variants AST representation and there seem to be quite a lot of cases but at least handling the basic ones should do.

Make `default` a thunk?

The fact that default is a value makes it static. In some cases, like generating random values and incrementing IDs, it would be practical to customize default so that it can return a different value for a given factory function (I guess it could even be the default behavior, but it's not my issue here).

This can be worked around by creating factory functions manually but I think this defeats the purpose of this PPX.

So, would it make sense to be able to define default as function?

let default () =
  ...

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.