Skip to main content

ConcatProduct

Splicing one type-level product list onto the end of another.

Overview​

In Context-Generic Programming (CGP), describing the result of combining two records means naming their combined shape, and the shapes are type-level lists, a Product! of named fields each. ConcatProduct<Items> names the list you get by following one product with another.

It is the general form of AppendProduct: append is the single-entry special case of concat, and that is the shortest way to hold both in mind. Like append, CGP itself never uses it; it is provided for generic code of your own.

Definition​

ConcatProduct is parameterized by the second product and exposes the joined list as an associated type:

pub trait ConcatProduct<Items> {
type Output;
}

Self is the first product, Items is the second, and Output is the first's entries followed by the second's, in order. The trait lacks a method, because nothing needs executing: Output is an associated type the trait solver evaluates during type checking, so it costs nothing at run time.

Usage​

It is not in the prelude. Import it from cgp::core::field::traits:

use cgp::core::field::traits::ConcatProduct;

Two identities are worth knowing, because they let a generic bound resolve in the degenerate cases: concatenating onto Nil yields the other list unchanged, and concatenating Nil onto a list leaves it unchanged.

Examples​

The results are types, so the check is a type equality, here for the worked concatenation and the two Nil identities:

use cgp::core::field::traits::{AppendProduct, ConcatProduct};
use cgp::core::field::types::{Field, Nil};
use cgp::prelude::*;

pub type Base = Product![Field<Symbol!("host"), String>];

pub type WithPort = <Base as AppendProduct<Field<Symbol!("port"), u16>>>::Output;

pub type Extra = Product![Field<Symbol!("tls"), bool>];

pub type Full = <WithPort as ConcatProduct<Extra>>::Output;

/// The page's claim about `Full`.
pub fn assert_full(
fields: Full,
) -> Product![
Field<Symbol!("host"), String>,
Field<Symbol!("port"), u16>,
Field<Symbol!("tls"), bool>,
] {
fields
}

/// The `Nil` identities the page states under *Using it*.
pub fn assert_left_identity(fields: <Nil as ConcatProduct<Base>>::Output) -> Base {
fields
}

pub fn assert_right_identity(fields: <Base as ConcatProduct<Nil>>::Output) -> Base {
fields
}

This is the type-level counterpart of what CanBuildFrom does with values: this names the combined shape, that moves the fields into it.

When to use it​

Reach for it when generic code must name the shape produced by combining two others (a merge, a routine that extends a record with a caller-supplied set of fields) and essentially never otherwise.

  • Reach for AppendProduct for a single entry. It is the same recursion with a one-element graft, and it reads better for the one-field case.
  • Use HasFields instead if you only need a type's existing shape.
  • Use CanBuildFrom instead if you want the values merged rather than the shapes named. This trait never touches a value.
  • Note that ConcatPath is the path-level analogue, doing the same job for the segment lists behind Path!, with the same two-impl recursion.

ConcatSum does not exist. Combining two sums is not an operation this layer provides.

Under the hood​

Two impls, one per list node. Each head is kept and the tail rebuilt; at the terminator, the whole second list is substituted:

impl<Items> ConcatProduct<Items> for Nil {
type Output = Items; // the only difference from AppendProduct
}

impl<Head, Tail, Items> ConcatProduct<Items> for Cons<Head, Tail>
where
Tail: ConcatProduct<Items>,
{
type Output = Cons<Head, Tail::Output>;
}

Set Items = Cons<Item, Nil> and this is AppendProduct exactly, which is the precise sense in which append is a special case.

The recursion walks the first list only, so its cost is proportional to the left operand's length rather than to the result's.

Common Mistakes​

It is not in the prelude. Import from cgp::core::field::traits.

The result is Output, while MapFields exposes Mapped. The asymmetry between the three product operations is easy to forget.

It is product-only. For paths, the analogue is ConcatPath; for sums, there is nothing.

Duplicate entries are not detected. Concatenating two products that share a field name yields a list with the name twice, which is a legal type and will fail later, at whatever consumes it, rather than here.

Order is part of the type. A then B is a different type from B then A, and nothing normalizes them.

A long list means a deep recursion, so concatenating two wide shapes costs compile time proportional to the first one's width.

The ideas behind it:

Source​


An AI agent wrote this page using the CGP knowledge base. Its content was verified against the library's source. See How AI is used in this project.