Skip to content

Commit 543e837

Browse files
mediremiYawarRaza7349
authored andcommitted
Show how to use GADTs with inline function type annotations (#1103)
1 parent 71c9f55 commit 543e837

File tree

2 files changed

+50
-50
lines changed

2 files changed

+50
-50
lines changed

pages/docs/manual/v11.0.0/generalized-algebraic-data-types.mdx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ Sometimes, a function should have a different return type based on what you give
118118
```res example
119119
type rec number<_> = Int(int): number<int> | Float(float): number<float>
120120
121-
let add:
122-
type a. (number<a>, number<a>) => a =
123-
(a, b) =>
124-
switch (a, b) {
125-
| (Int(a), Int(b)) => a + b
126-
| (Float(a), Float(b)) => a +. b
127-
}
121+
let add = (type a, x: number<a>, y: number<a>): a =>
122+
switch (x, y) {
123+
| (Int(x), Int(y)) => x + y
124+
| (Float(x), Float(y)) => x +. y
125+
}
128126
129127
let foo = add(Int(1), Int(2))
130128
@@ -145,26 +143,28 @@ module IfNotFound = {
145143
| DefaultTo('a): t<'a, 'a>
146144
}
147145
148-
let flexible_find:
149-
type a b. (~f: a => bool, array<a>, IfNotFound.t<a, b>) => b =
150-
(~f, arr, ifNotFound) => {
151-
open IfNotFound
152-
switch Array.find(arr, f) {
153-
| None =>
154-
switch ifNotFound {
155-
| Raise => failwith("No matching item found")
156-
| ReturnNone => None
157-
| DefaultTo(x) => x
158-
}
159-
| Some(x) =>
160-
switch ifNotFound {
161-
| ReturnNone => Some(x)
162-
| Raise => x
163-
| DefaultTo(_) => x
164-
}
146+
let flexible_find = (
147+
type a b,
148+
~f: a => bool,
149+
arr: array<a>,
150+
ifNotFound: IfNotFound.t<a, b>,
151+
): b => {
152+
open IfNotFound
153+
switch Array.find(arr, f) {
154+
| None =>
155+
switch ifNotFound {
156+
| Raise => failwith("No matching item found")
157+
| ReturnNone => None
158+
| DefaultTo(x) => x
159+
}
160+
| Some(x) =>
161+
switch ifNotFound {
162+
| ReturnNone => Some(x)
163+
| Raise => x
164+
| DefaultTo(_) => x
165165
}
166166
}
167-
167+
}
168168
```
169169

170170
## Hide and recover Type information Dynamically

pages/docs/manual/v12.0.0/generalized-algebraic-data-types.mdx

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,11 @@ Sometimes, a function should have a different return type based on what you give
118118
```res example
119119
type rec number<_> = Int(int): number<int> | Float(float): number<float>
120120
121-
let add:
122-
type a. (number<a>, number<a>) => a =
123-
(a, b) =>
124-
switch (a, b) {
125-
| (Int(a), Int(b)) => a + b
126-
| (Float(a), Float(b)) => a +. b
127-
}
121+
let add = (type a, x: number<a>, y: number<a>): a =>
122+
switch (x, y) {
123+
| (Int(x), Int(y)) => x + y
124+
| (Float(x), Float(y)) => x +. y
125+
}
128126
129127
let foo = add(Int(1), Int(2))
130128
@@ -145,26 +143,28 @@ module IfNotFound = {
145143
| DefaultTo('a): t<'a, 'a>
146144
}
147145
148-
let flexible_find:
149-
type a b. (~f: a => bool, array<a>, IfNotFound.t<a, b>) => b =
150-
(~f, arr, ifNotFound) => {
151-
open IfNotFound
152-
switch Array.find(arr, f) {
153-
| None =>
154-
switch ifNotFound {
155-
| Raise => failwith("No matching item found")
156-
| ReturnNone => None
157-
| DefaultTo(x) => x
158-
}
159-
| Some(x) =>
160-
switch ifNotFound {
161-
| ReturnNone => Some(x)
162-
| Raise => x
163-
| DefaultTo(_) => x
164-
}
146+
let flexible_find = (
147+
type a b,
148+
~f: a => bool,
149+
arr: array<a>,
150+
ifNotFound: IfNotFound.t<a, b>,
151+
): b => {
152+
open IfNotFound
153+
switch Array.find(arr, f) {
154+
| None =>
155+
switch ifNotFound {
156+
| Raise => failwith("No matching item found")
157+
| ReturnNone => None
158+
| DefaultTo(x) => x
159+
}
160+
| Some(x) =>
161+
switch ifNotFound {
162+
| ReturnNone => Some(x)
163+
| Raise => x
164+
| DefaultTo(_) => x
165165
}
166166
}
167-
167+
}
168168
```
169169

170170
## Hide and recover Type information Dynamically

0 commit comments

Comments
 (0)