Improve sum type docs

pull/664/head
Arijit Basu 10 months ago
parent f84d9d5c6a
commit 859d888bde
No known key found for this signature in database
GPG Key ID: 0F8EF5258DC38077

@ -35,8 +35,19 @@ 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.
e.g.
```rust
enum Result {
Ok,
Err,
Pending,
}
```
We'd document it here as:
@ -44,6 +55,7 @@ We'd document it here as:
>
> - "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:
@ -51,24 +63,36 @@ to associate each branch of the enum with further nested types like:
```rust
enum Result {
Ok(bool),
Err(String),
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…
Cancel
Save