Example:
Here’s part of the implementation of Js.Json in js_json.ml:
(** Efficient JSON encoding using JavaScript API *)
type t
type _ kind =
| String : Js_string.t kind
| Number : float kind
| Object : t Js_dict.t kind
| Array : t array kind
| Boolean : bool kind
| Null : Js_types.null_val kind
type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString of string
| JSONNumber of float
| JSONObject of t Js_dict.t
| JSONArray of t array
let classify (x : t) : tagged_t =
let ty = Js.typeof x in
if ty = "string" then
JSONString (Obj.magic x)
else if ty = "number" then
JSONNumber (Obj.magic x )
else if ty = "boolean" then
if (Obj.magic x) = true then JSONTrue
else JSONFalse
else if (Obj.magic x) == Js.null then
JSONNull
else if Js_array2.isArray x then
JSONArray (Obj.magic x)
else
JSONObject (Obj.magic x)
and here is how it looks instead:
@unboxed
type rec t =
| @as(false) False
| @as(true) True
| @as(null) Null
| String(string)
| Number(float)
| Object(Js.Dict.t<t>)
| Array(array<t>)
type tagged_t =
| JSONFalse
| JSONTrue
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(Js.Dict.t<t>)
| JSONArray(array<t>)
let classify = (x: t) =>
switch x {
| False => JSONFalse
| True => JSONTrue
| Null => JSONNull
| String(s) => JSONString(s)
| Number(n) => JSONNumber(n)
| Object(o) => JSONObject(o)
| Array(a) => JSONArray(a)
}


