cgp_field/types/
sum.rs

1/**
2    The `Either` type, a.k.a. `σ`, is used to represent an _anonymous sum type_.
3
4    Similar to [`Cons`](crate::types::Cons), `Either` is used to form a sum type
5    by combining a chain of `Either` types, and terminated with a [`Void`] type.
6    But unlike product types, a sum type has values that belong to one
7    of the variants in the list.
8
9    `Either` is also shown as `σ`, together with [`Void`] shown as `θ`, to improve
10    the readability of compiler error messages. Through the shortened name, a sum
11    type would take slightly less space, making it more likely to fit on a single
12    line for the user to read what the type is.
13
14    `Either` is most often used through the `Sum!` macro, which accepts a list of
15    types and turns them into a chain of `Either` types.
16
17    ## Example
18
19    Given the following sum type definition:
20
21    ```rust,ignore
22    type MyUnion = Sum![u32, String, bool];
23    ```
24
25    The following type would be generated:
26
27    ```rust,ignore
28    type MyUnion = Either<u32, Either<String, Either<bool, Void>>>;
29    ```
30
31    which would be shown with the shortened representation as:
32
33    ```rust,ignore
34    type MyUnion = σ<u32, σ<String, σ<bool, θ>>>;
35    ```
36*/
37#[derive(Eq, PartialEq, Debug, Clone)]
38pub enum σ<Head, Tail> {
39    Left(Head),
40    Right(Tail),
41}
42
43/**
44    The `Void` type, a.k.a. `θ`, is used to represent the end of an _anonymous sum type_,
45    or an _empty_ sum type.
46
47    `Void` is commonly used as the `Tail` of a [`Either`] type, to terminate the list.
48    When used on its own, it represents an empty sum type, which can _never be constructed_.
49
50    `Void` is functionally the same as the
51    [_never_ type](https://doc.rust-lang.org/std/primitive.never.html), `!`,
52    or otherwise
53    [`Infallible`](https://doc.rust-lang.org/std/convert/enum.Infallible.html).
54    However, we define a separate `Void` type, to make it more clear that it is
55    specifically used for terminating a sum type.
56
57    Read more about sum types in [`Either`].
58*/
59#[derive(Eq, PartialEq, Debug, Clone)]
60pub enum θ {}
61
62pub use as Void, σ as Either};