Skip to content
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

Use polymorphic variants for phantom types #34

Merged
merged 2 commits into from
Aug 4, 2017

Conversation

talex5
Copy link
Collaborator

@talex5 talex5 commented Aug 3, 2017

Instead of declaring a load of unique type names at the start of the file, use polymorphic variants. These don't need to be declared before use.

e.g.

type struct_Foo_11668594054516559475
type reader_t_Foo_11668594054516559475 = struct_Foo_11668594054516559475 reader_t
type builder_t_Foo_11668594054516559475 = struct_Foo_11668594054516559475 builder_t

module Foo : sig
  type t = reader_t_Foo_11668594054516559475
  type builder_t = builder_t_Foo_11668594054516559475
  val of_builder : builder_t -> t

becomes

module Foo : sig
  type struct_t = [`Foo_a1ef2c783fb1ce73]
  type t = struct_t reader_t
  val of_builder : struct_t builder_t -> t

This simplifies imports (all uses of Obj.magic have been removed from the generated code).

It also makes it possible to refer to a type before applying the generated code's functor. This might be useful for the RPC system, as it allows it to define a capability as something that takes a request struct, before applying the RPC schema's functor (that defines what a request struct is).

I also switched to formatting the node ID as hex, to make it shorter and to match the format used in the schema files.

Note that there is a slight API change here, because the $Struct.reader_t and $Struct.builder_t types have gone (to avoid confusion with the generic reader_t and builder_t types). You have to use struct_t reader_t and struct_t builder_t instead.

To make writing generic code simpler, there are two new helpers in StructStorage:

val reader_of_builder : 'a builder_t -> 'a reader_t
val message_of_builder : _ builder_t -> rw Message.t

This required some changes to the benchmark code, but the new code is shorter (6 files changed, 29 insertions(+), 57 deletions(-)).

@talex5 talex5 force-pushed the poly-types branch 3 times, most recently from ed2e18b to ffdc2fd Compare August 3, 2017 12:34
Instead of declaring a load of unique type names at the start of the
file, use polymorphic variants. These don't need to be declared before
use.

e.g.

    type struct_Foo_11668594054516559475
    type reader_t_Foo_11668594054516559475 = struct_Foo_11668594054516559475 reader_t
    type builder_t_Foo_11668594054516559475 = struct_Foo_11668594054516559475 builder_t

    module Foo : sig
      type t = reader_t_Foo_11668594054516559475
      type builder_t = builder_t_Foo_11668594054516559475
      val of_builder : builder_t -> t

becomes

    module Foo : sig
      type struct_t = [`Foo_a1ef2c783fb1ce73]
      type t = struct_t reader_t
      val of_builder : struct_t builder_t -> t

This simplifies imports (all uses of `Obj.magic` have been removed from
the generated code).

It also makes it possible to refer to a type before applying the
generated code's functor. This might be useful for the RPC system, as
it allows it to define a capability as something that takes a request
struct, before applying the RPC schema's functor (that defines what a
request struct is).

I also switched to formatting the node ID as hex, to make it shorter and
to match the format used in the schema files.

Note that there is a slight API change here, because the `$Struct.reader_t`
and `$Struct.builder_t` types have gone (to avoid confusion with the
generic `reader_t` and `builder_t` types). You have to use `struct_t reader_t`
and `struct_t builder_t` instead.

To make writing generic code simpler, there are two new helpers in
StructStorage:

    val reader_of_builder : 'a builder_t -> 'a reader_t
    val message_of_builder : _ builder_t -> rw Message.t

This required some changes to the benchmark code, but the new code is
shorter (`6 files changed, 29 insertions(+), 57 deletions(-)`).
@talex5 talex5 merged commit e757430 into capnproto:master Aug 4, 2017
@talex5 talex5 deleted the poly-types branch August 4, 2017 08:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant