cryptosense / ppx_factory Goto Github PK
View Code? Open in Web Editor NEWOCaml preprocessor to derive factory methods and default values from type definitions
License: BSD 2-Clause "Simplified" License
OCaml preprocessor to derive factory methods and default values from type definitions
License: BSD 2-Clause "Simplified" License
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
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
We should derive a default for (a, b) result
types if we can derive a default for at least one of a
or b
.
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.
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.
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 () =
...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.