Ad some docs to tag union layouts

This commit is contained in:
Richard Feldman 2022-05-13 13:06:33 -04:00
parent 7771b2090c
commit c9f06a9172
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798

View File

@ -276,6 +276,12 @@ pub enum UnionLayout<'a> {
/// It has more than one other variant, so they need tag IDs (payloads are "wrapped")
/// e.g. `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]`
/// see also: https://youtu.be/ip92VMpf_-A?t=164
///
/// nullable_id refers to the index of the tag that is represented at runtime as NULL.
/// For example, in `FingerTree a : [ Empty, Single a, More (Some a) (FingerTree (Tuple a)) (Some a) ]`,
/// the ids would be Empty = 0, More = 1, Single = 2, because that's how those tags are
/// ordered alphabetically. Since the Empty tag will be represented at runtime as NULL,
/// and since Empty's tag id is 0, here nullable_id would be 0.
NullableWrapped {
nullable_id: u16,
other_tags: &'a [&'a [Layout<'a>]],
@ -283,6 +289,14 @@ pub enum UnionLayout<'a> {
/// A recursive tag union with only two variants, where one is empty.
/// Optimizations: Use null for the empty variant AND don't store a tag ID for the other variant.
/// e.g. `ConsList a : [ Nil, Cons a (ConsList a) ]`
///
/// nullable_id is a bool because it's only ever 0 or 1, but (as with the NullableWrapped
/// variant), it reprsents the index of the tag that will be represented at runtime as NULL.
///
/// So for example, in `ConsList a : [ Nil, Cons a (ConsList a) ]`, Nil is tag id 1 and
/// Cons is tag id 0 because Nil comes alphabetically after Cons. Here, Nil will be
/// represented as NULL at runtime, so nullable_id is 1 - which is to say, `true`, because
/// `(1 as bool)` is `true`.
NullableUnwrapped {
nullable_id: bool,
other_fields: &'a [Layout<'a>],