Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions pages/docs/manual/v11.0.0/generalized-algebraic-data-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ Sometimes, a function should have a different return type based on what you give
```res example
type rec number<_> = Int(int): number<int> | Float(float): number<float>

let add:
type a. (number<a>, number<a>) => a =
(a, b) =>
switch (a, b) {
| (Int(a), Int(b)) => a + b
| (Float(a), Float(b)) => a +. b
}
let add = (type a, x: number<a>, y: number<a>): a =>
switch (x, y) {
| (Int(x), Int(y)) => x + y
| (Float(x), Float(y)) => x +. y
}

let foo = add(Int(1), Int(2))

Expand All @@ -145,26 +143,28 @@ module IfNotFound = {
| DefaultTo('a): t<'a, 'a>
}

let flexible_find:
type a b. (~f: a => bool, array<a>, IfNotFound.t<a, b>) => b =
(~f, arr, ifNotFound) => {
open IfNotFound
switch Array.find(arr, f) {
| None =>
switch ifNotFound {
| Raise => failwith("No matching item found")
| ReturnNone => None
| DefaultTo(x) => x
}
| Some(x) =>
switch ifNotFound {
| ReturnNone => Some(x)
| Raise => x
| DefaultTo(_) => x
}
let flexible_find = (
type a b,
~f: a => bool,
arr: array<a>,
ifNotFound: IfNotFound.t<a, b>,
): b => {
open IfNotFound
switch Array.find(arr, f) {
| None =>
switch ifNotFound {
| Raise => failwith("No matching item found")
| ReturnNone => None
| DefaultTo(x) => x
}
| Some(x) =>
switch ifNotFound {
| ReturnNone => Some(x)
| Raise => x
| DefaultTo(_) => x
}
}

}
```

## Hide and recover Type information Dynamically
Expand Down
50 changes: 25 additions & 25 deletions pages/docs/manual/v12.0.0/generalized-algebraic-data-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,11 @@ Sometimes, a function should have a different return type based on what you give
```res example
type rec number<_> = Int(int): number<int> | Float(float): number<float>

let add:
type a. (number<a>, number<a>) => a =
(a, b) =>
switch (a, b) {
| (Int(a), Int(b)) => a + b
| (Float(a), Float(b)) => a +. b
}
let add = (type a, x: number<a>, y: number<a>): a =>
switch (x, y) {
| (Int(x), Int(y)) => x + y
| (Float(x), Float(y)) => x +. y
}

let foo = add(Int(1), Int(2))

Expand All @@ -145,26 +143,28 @@ module IfNotFound = {
| DefaultTo('a): t<'a, 'a>
}

let flexible_find:
type a b. (~f: a => bool, array<a>, IfNotFound.t<a, b>) => b =
(~f, arr, ifNotFound) => {
open IfNotFound
switch Array.find(arr, f) {
| None =>
switch ifNotFound {
| Raise => failwith("No matching item found")
| ReturnNone => None
| DefaultTo(x) => x
}
| Some(x) =>
switch ifNotFound {
| ReturnNone => Some(x)
| Raise => x
| DefaultTo(_) => x
}
let flexible_find = (
type a b,
~f: a => bool,
arr: array<a>,
ifNotFound: IfNotFound.t<a, b>,
): b => {
open IfNotFound
switch Array.find(arr, f) {
| None =>
switch ifNotFound {
| Raise => failwith("No matching item found")
| ReturnNone => None
| DefaultTo(x) => x
}
| Some(x) =>
switch ifNotFound {
| ReturnNone => Some(x)
| Raise => x
| DefaultTo(_) => x
}
}

}
```

## Hide and recover Type information Dynamically
Expand Down