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

ppx: derive signatures #32

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

ppx: derive signatures #32

wants to merge 6 commits into from

Conversation

andreypopp
Copy link
Collaborator

No description provided.

Copy link
Member

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, impressive how fast you fixed it. Thanks!

@@ -11,11 +11,16 @@ class virtual deriving : object
loc:location -> path:label -> core_type -> expression
(** a deriver can be applied to as type expression as extension node. *)

method virtual generator :
method virtual str_type_decl :
ctxt:Expansion_context.Deriver.t ->
rec_flag * type_declaration list ->
structure
(** or it can be attached to a type declaration. *)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Off-topic, but I don't understand this comment.

| Ptyp_var txt -> { txt; loc }
| _ ->
Location.raise_errorf ~loc
"type variable is not a variable"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like:

Suggested change
"type variable is not a variable"
"unsupported type description: only type variables such as `'a` are supported"

(feel free to ignore, i saw this error msg was used already)

let name =
match t.ptyp_desc with
| Ptyp_var txt -> { txt; loc }
| _ ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do derive_of_type_declaration and gen_type_ascription support Ptyp_any but not here?

Edit: oh, because we need a name to keep drilling I guess 🤔

@jchavarri
Copy link
Member

@andreypopp is this good to merge? none of my comments is blocking.

@jchavarri
Copy link
Member

Ah, we're missing a changelog entry I guess.

@andreypopp
Copy link
Collaborator Author

Actually I think this doesn’t work properly in case you are extending a polymorphic variant decoder. This is because it generates an extra function with _poly suffix which is not being exposed in signatures.

@@ -732,52 +586,6 @@ module Conv = struct
:: next)
in
pexp_match ~loc x cases

method! derive_of_type_declaration td =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what exactly are we deleting? aren't we reducing the scope of what's supported?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i thought we would be losing the polyvar composition, but from what i could check (i tested the test in the comment below), we don't, the test keeps passing fine.

Copy link
Collaborator Author

@andreypopp andreypopp Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m going to work a bit more on the last commit but the gist is — instead of generating a separate function for polyvariants which decodes into None on unmatched tag, we extend error type to have a case for unmatched tag. This is why we removed code here.

@@ -13,7 +13,11 @@ third-party libraries.
type 'a decoder = Js.Json.t -> 'a
(** The type of a decoder combinator *)

exception DecodeError of string
type error = Json_error of string | Unpexpected_variant of string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem that the second branch of the variant is used, but:

Suggested change
type error = Json_error of string | Unpexpected_variant of string
type error = Json_error of string | Unexpected_variant of string

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second case is generated by ppx

@andreypopp
Copy link
Collaborator Author

I think this can be merged once we figure out the issue with melc in be1e907

@jchavarri
Copy link
Member

ftr melange-atdgen-codec-runtime, which depends on melange-json, breaks when I try to install using commit c669c07 above:

# File "src/atdgen_codec_decode.ml", line 1:
# Error: This expression has type string but an expression was expected of type
#          error = Json__Json_decode.error

@andreypopp
Copy link
Collaborator Author

yeah, that's a breaking change...

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.

3 participants