Last active
April 22, 2020 14:32
-
-
Save hartmut27/8c883632c77925da6431b1e16994fa65 to your computer and use it in GitHub Desktop.
1) GADT, 2) Variants in usage of ReasonML/OCaml pipes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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