-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for generating asynchronous bindings using Lwt jobs #391
Conversation
…ed code. (This does not include any actual support for concurrency.)
…ivial return types.
I am curious why you used (btw, that PR is great!) |
It's a workaround for a restriction in the type system. If you have an abstract type with a parameter, like this type 'a foo then you're not allowed to use it to define a GADT that uses 'foo' in the return type of one of the constructors, like this type 'a t = T : 'a -> 'a foo t Allowing that definition would cause problems if type 'a foo = int The restriction was introduced in OCaml 4.01.0. Here's an example using OCaml 4.00.1 showing what goes wrong: $ cat inj.ml
module F (X: sig type 'a foo end) =
struct
type 'a t = T : 'a -> 'a X.foo t
let unT : type a. a X.foo t -> a = function (T x) -> x
end
include F(struct type 'a foo = int end)
let () = print_endline (unT (T 0))
$ ocaml inj.ml
Segmentation fault The module generated by type 'a fn =
Returns : 'a typ -> 'a return fn
| Function : 'a typ * 'b fn -> ('a -> 'b) fn This definition of |
Thanks! |
This pull request adds a new option for binding C functions, namely support for the Lwt jobs framework, allowing bound functions to execute asynchronously.
The changes to the interface are fairly minor, to the extent that existing bindings descriptions can be used unchanged.
The existing interface
For example, here is a functor which binds two functions,
puts
andrmdir
:With the existing interface you can generate C and ML by passing
Bindings
to thewrite_c
andwrite_ml
functions, like this:These calls generate a set of C functions and an OCaml module (say "
Generated_module
") containing code that bindsputs
andrmdir
. ApplyingBindings
toGenerated_module
builds a module containing bindings to those functions, ready for use in an OCaml program:The new interface
To create asynchronous bindings to
puts
andrmdir
, most of the pieces above, including theBindings
functor, can be reused. The only change needed is an additional argument towrite_c
andwrite_ml
:The type of the generated module ("
Generated_lwt_module
", say) is slightly different, reflecting the fact that the bindings areLwt
jobs rather than synchronous functions:The type
Generated_lwt_module.return
is a simple wrapper aroundLwt.t
:After projecting out the
lwt
field the results of the functions can be used with theLwt
library in the usual way: