{"action":"closed","issue":{"url":"https://api.github.com/repos/whitequark/ppx_deriving_yojson/issues/24","labels_url":"https://api.github.com/repos/whitequark/ppx_deriving_yojson/issues/24/labels{/name}","comments_url":"https://api.github.com/repos/whitequark/ppx_deriving_yojson/issues/24/comments","events_url":"https://api.github.com/repos/whitequark/ppx_deriving_yojson/issues/24/events","html_url":"https://github.com/whitequark/ppx_deriving_yojson/issues/24","id":66247209,"number":24,"title":"Problem with recursive polymorphic variants","user":{"login":"smondet","id":617111,"avatar_url":"https://avatars.githubusercontent.com/u/617111?v=3","gravatar_id":"","url":"https://api.github.com/users/smondet","html_url":"https://github.com/smondet","followers_url":"https://api.github.com/users/smondet/followers","following_url":"https://api.github.com/users/smondet/following{/other_user}","gists_url":"https://api.github.com/users/smondet/gists{/gist_id}","starred_url":"https://api.github.com/users/smondet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smondet/subscriptions","organizations_url":"https://api.github.com/users/smondet/orgs","repos_url":"https://api.github.com/users/smondet/repos","events_url":"https://api.github.com/users/smondet/events{/privacy}","received_events_url":"https://api.github.com/users/smondet/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-04-03T23:24:39Z","updated_at":"2015-04-06T18:56:38Z","closed_at":"2015-04-06T18:56:38Z","body":"\r\nHere below we see that the generated function `c_of_yojson` does not return `` [ `Ok of c | `Error of string ]``:\r\n\r\n```ocaml\r\nutop> type a = [ `B of string ] \r\n                  [@@deriving of_yojson];;\r\n\r\ntype a = [ `B of id ]\r\nval a_of_yojson :\r\n  Yojson.Safe.json -> [> `Error of id | `Ok of [> `B of string ] ] = <fun>\r\n\r\nutop> type b = [a | `C of b list]\r\n          [@@deriving of_yojson];;\r\n\r\ntype b = [ `B of id | `C of b list ]\r\nval b_of_yojson :\r\n  Yojson.Safe.json ->\r\n  [ `Error of id | `Ok of [> `B of string | `C of 'a list ] as 'a ] = <fun>\r\n\r\nutop> type c = [ a | b | `D of b list]\r\n          [@@deriving of_yojson]\r\n          let c_of_yojson yj : [ `Ok of c | `Error of string ] = c_of_yojson yj;;\r\n(* We try to constrain the type here. *)\r\n\r\nError: This expression has type\r\n         [> `Error of id\r\n          | `Ok of [> `B of string | `C of 'a list | `D of b list ] as 'a ]\r\n       but an expression was expected of type [ `Error of string | `Ok of c ]\r\n       Type 'a is not compatible with type\r\n         c = [ `B of string | `C of b list | `D of b list ] \r\n       Type c = [ `B of string | `C of b list | `D of b list ]\r\n       is not compatible with type b = [ `B of string | `C of b list ] \r\n       The second variant type does not allow tag(s) `D\r\n```\r\n\r\nNote that if we “*inline”*  the  defintion of `c` it works again:\r\n\r\n```ocaml\r\nutop> type c = [ `B of string | `C of b list | `D of b list ]\r\n          [@@deriving of_yojson]\r\n          let c_of_yojson yj : [ `Ok of c | `Error of string ] = c_of_yojson yj  ;;\r\n\r\ntype c = [ `B of string | `C of b list | `D of b list ]\r\nval c_of_yojson : Yojson.Safe.json -> [ `Error of string | `Ok of c ] = <fun>\r\n```\r\n\r\nWith `` type b = [a | `C of something_else list]  ```  being not recursive, it also works.\r\n\r\n\r\n\r\nPS: I'd be interested in any other work around that problem, because my real-world use case is pretty big and *inlining* all the subtyping relationships would be very annoying.\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n"}}