Skip to content

Instantly share code, notes, and snippets.

@hartmut27
Last active April 22, 2020 14:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hartmut27/8c883632c77925da6431b1e16994fa65 to your computer and use it in GitHub Desktop.
Save hartmut27/8c883632c77925da6431b1e16994fa65 to your computer and use it in GitHub Desktop.
1) GADT, 2) Variants in usage of ReasonML/OCaml pipes
/*
* Variants in usage of ReasonML/OCaml pipes
*/
type attributeType('t) =
| Lastname: attributeType(string)
| ZipCode: attributeType(int);
type recordType = {
lastname: string,
zipCode: int,
};
let getString: type t. (recordType, attributeType(t)) => string =
(record, attr) =>
switch (attr) {
| Lastname => record.lastname
| ZipCode => string_of_int(record.zipCode)
};
// example record
let john = {lastname: "John", zipCode: 8500};
// 9 variants of the same result:
// getting John's ZipCode as a string
let _: string = getString(john, ZipCode);
let _: string = john->getString(ZipCode);
let _: string = john->getString @@ ZipCode;
let _: string = (john |> getString)(ZipCode);
let _: string = (john |> getString) @@ ZipCode;
let _: string = ZipCode |> getString @@ john;
let _: string = ZipCode |> getString(john);
let _: string = ZipCode |> john->getString;
let _: string = ZipCode |> (john |> getString);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment