Type that depends on VALUE?

Can someone explain the OCaml side of the following blob of code:

type _ response =
  | ArrayBuffer : Typed_array.arrayBuffer t Opt.t response
  | Blob : #File.blob t Opt.t response
  | Document : Dom.element Dom.document t Opt.t response
  | JSON : 'a Opt.t response
  | Text : js_string t response
  | Default : string response

https://github.com/ocsigen/js_of_ocaml/blob/master/lib/js_of_ocaml/xmlHttpRequest.ml#L31

I understand the JS side fine. When doing a XMLHttpRequest - Web APIs | MDN
we can set the type of respone we want.

Here is what I do not understand:

perform_raw appears to return something of type:
type 'response generic_http_frame =
  { url : string
  ; code : int
  ; headers : string -> string option
  ; content : 'response
  ; content_xml : unit -> Dom.element Dom.document t option
  }

Here is the crazy thing. This starts to look like dependent types.

When I pass in a VALUE of Default, I get a TYPE of string response
When I pass in a VALUE of ArrayBuffer, I get a TYPE of Typed_array.arrayBuffer t Opt.t response

This looks like a TYPE that DEPENDS on a VALUE.

Does OCaml support DEPENDENT TYPES, or if not, what I misunderstanding ?

LINKS:
perform_raw:

response type:

OCaml supports a particular case of dependent types called generalized algebraic datatypes (GADT). Learn more at OCaml - Generalized algebraic datatypes and GADTs - Real World OCaml.

Cheers,
Nicolas

3 Likes