mirror of
https://github.com/sayanarijit/xplr.git
synced 2024-09-11 11:05:39 +03:00
Improve sum type docs
This commit is contained in:
parent
f84d9d5c6a
commit
859d888bde
@ -35,40 +35,64 @@ enum Result {
|
||||
}
|
||||
```
|
||||
|
||||
Here, `Result` can be one of two the possible values: `Ok` and `Err` (just like
|
||||
`boolean`, but tagged).
|
||||
Here, `Result` can be one of two possible set of values: `Ok` and `Err`, just
|
||||
like `boolean`, but unlike `boolean`, being tagged allows `Result` to have more
|
||||
than two variants if required, by changing the definition.
|
||||
|
||||
We'd document it here as:
|
||||
|
||||
> Result is a sum type that can be one of the following:
|
||||
>
|
||||
> - "Ok"
|
||||
> - "Err"
|
||||
|
||||
But some languages (like Rust, Haskell, Elm etc.) go even further, allowing us
|
||||
to associate each branch of the enum with further nested types like:
|
||||
e.g.
|
||||
|
||||
```rust
|
||||
enum Result {
|
||||
Ok(bool),
|
||||
Err(String),
|
||||
Ok,
|
||||
Err,
|
||||
Pending,
|
||||
}
|
||||
```
|
||||
|
||||
We'd document it here as:
|
||||
|
||||
> Result is a sum type that can be one of the following:
|
||||
>
|
||||
> - "Ok"
|
||||
> - "Err"
|
||||
> - "Pending"
|
||||
|
||||
But some languages (like Rust, Haskell, Elm etc.) go even further, allowing us
|
||||
to associate each branch of the enum with further nested types like:
|
||||
|
||||
```rust
|
||||
enum Result {
|
||||
Ok(bool),
|
||||
Err(Error),
|
||||
Pending,
|
||||
}
|
||||
```
|
||||
|
||||
Here, as we can see, unlike the first example, some of `Result`'s possible
|
||||
variants can have further nested types associated with them. Note that `Error`
|
||||
here can be either a sum type (e.g. enum), or a product type (e.g.
|
||||
class/struct), but whatever it is, it will only exist when `Result` is `Err`.
|
||||
|
||||
We'd document it here as:
|
||||
|
||||
> Result is a sum type that can be one of the following:
|
||||
>
|
||||
> - { Ok = bool }
|
||||
> - { Err = "string" }
|
||||
> - { Err = Error }
|
||||
> - "Pending"
|
||||
|
||||
Here, `Result` still has only two possibilities, but unlike the first example,
|
||||
each possibility here has further set of possible value(s).
|
||||
And then we'd go on documenting whatever `Error` is.
|
||||
|
||||
And there you go. This is exactly what sum types are - glorified enums that can
|
||||
So, there you go. This is exactly what sum types are - glorified enums that can
|
||||
have nested types in each branch.
|
||||
|
||||
---
|
||||
|
||||
If you're still confused about something, or if you found an error in this
|
||||
explaination, feel free to [discuss together][5].
|
||||
|
||||
[1]: https://en.wikipedia.org/wiki/Tagged_union
|
||||
[2]: layout.md
|
||||
[3]: message.md
|
||||
[4]: style.md#color
|
||||
[5]: community.md
|
||||
|
Loading…
Reference in New Issue
Block a user