melange-community / melange-community/jsonkit

type errors in handwritten conversion function result in weird error

Open
#69 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
OCaml
Stars
24
Forks
7
Avg merge
1d 15h
Merged PRs (30d)
5

Description

type mytype = Int64.t

let mytype_of_json = function
  | `Float f -> Int64.of_float f
  | json -> raise (Melange_json.of_json_error ~json "expected a float")

let mytype_to_json i = Int64.to_string i

type my_record = {
  a : string;
  my_value : mytype;
  b : string;
}
[@@deriving json]

gives the error :

Error: This expression has type (string * [> `String of string ]) list
       but an expression was expected of type (string * string) list
       Type [> `String of string ] is not compatible with type string 

located on the whole of the my_record type definition.

This is quite confusing when the actual error is that mytype_to_json does not return a json but a string. I don't know if something can be done about the location of the error, but at a type constraint could be put on the usage of mytype_to_json so that is error is more like expected Melange_json.t, got string instead of mentioning a tuple and a list.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the handwritten mytype_to_json function and the my_record type's [@@deriving json] usage, then inspect how the deriving result reports this type mismatch. Reproduce the example and improve the diagnostic so it identifies that mytype_to_json returns string where a JSON value is expected, with a more useful location than the whole record definition.

Written by the indexing model from the issue text.

Assessment

Tech stack
ocaml
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.