open Mocha open Test_utils // An operator such as `+` takes its type from the left operand if that has a // known type, else from the right one, else int. The operand that gets the // type is given it after it has been typed once. Each function below only // compiles if its operands get the right type. // From the left operand let floatFromLeft = x => 1.5 + x let stringFromLeft = x => "a" + x // From the right operand let floatFromRight = x => x + 2.5 let bigintFromRight = x => x + 2n // Neither operand has a known type: int let intByDefault = (x, y) => x + y // The operand's type is int only inside the GADT branch, so it is typed // again against int, which succeeds type rec intWitness<_> = IsInt: intWitness let intInBranch = (type a, w: intWitness, x: a) => 1 + switch w { | IsInt => x } // Nested operators take the type through each level let nestedFloat = x => 1.5 + (x + (x + 0.5)) let nestedFromRight = x => x - (x - 1.5) describe(__MODULE__, () => { test("the type comes from the left operand", () => { eq(__LOC__, floatFromLeft(1.0), 2.5) eq(__LOC__, stringFromLeft("b"), "ab") }) test("the type comes from the right operand", () => { eq(__LOC__, floatFromRight(1.0), 3.5) eq(__LOC__, bigintFromRight(1n), 3n) }) test("without a known type the operator is on ints", () => { eq(__LOC__, intByDefault(1, 2), 3) }) test("an operand whose type is int only in a GADT branch", () => { eq(__LOC__, intInBranch(IsInt, 2), 3) }) test("nested operands", () => { eq(__LOC__, nestedFloat(1.0), 4.0) eq(__LOC__, nestedFromRight(2.0), 1.5) }) })