Skip to main content

WithType

Bind an abstract-type component to a concrete type, through the WithProvider adapter.

Overview​

Under Context-Generic Programming (CGP), an abstract type is chosen through wiring, and WithType<Type> is the alias WithProvider<UseType<Type>>. It binds an abstract-type component to the concrete Type on a context, the type that implements the trait, by adapting the foundational UseType<Type> provider through the WithProvider layer. It sets the same abstract type the plain UseType provider does, and both are interchangeable in wiring. Like every CGP provider, it carries no runtime value.

Usage​

WithType is not in the prelude. Import it from cgp::core::types. It takes one type parameter, the concrete type to supply, and appears as the value of an abstract-type component's wiring entry:

use cgp::core::types::WithType;

delegate_components! {
App {
ScalarTypeProviderComponent: WithType<f64>,
}
}

This sets the context's Scalar to f64, exactly as wiring UseType<f64> would.

Examples​

A complete use defines an abstract type and wires a concrete type through the alias:

use cgp::core::types::WithType;
use cgp::prelude::*;

#[cgp_type]
pub trait HasScalarType {
type Scalar: Copy;
}

pub struct App;

delegate_components! {
App {
ScalarTypeProviderComponent: WithType<f64>,
}
}

check_components! {
App {
ScalarTypeProviderComponent,
}
}

App wires ScalarTypeProviderComponent to WithType<f64>, so App implements HasScalarType with Scalar = f64. The check confirms that f64 meets the Copy bound on the associated type; without it, a type that breaks the bound is reported only when the component is used, as UseType shows.

When to use it​

Prefer the plain UseType<T> form. It binds the same type and is the idiomatic value for a #[cgp_type] component's wiring entry. The two are equivalent on every #[cgp_type] component, because the macro generates both a UseType impl and a WithProvider impl; WithType completes the With… family and reads as a single wiring choice where spelling out WithProvider<UseType<f64>> would not.

For an abstract type resolved through a table rather than fixed to one concrete type, use WithDelegatedType.

Under the hood​

WithType<Type> is a type alias:

pub type WithType<Type> = WithProvider<UseType<Type>>;

The WithProvider adapter forwards a component's provider-trait method to the inner provider's foundational method, and UseType<Type> is the foundational TypeProvider that reports Type as the abstract type. The generated WithProvider impl that makes this work is shown on the WithProvider page.

  • WithProvider — the adapter this alias specializes.
  • UseType — the inner provider it wraps, and the plain form to prefer.
  • WithDelegatedType — the sibling alias for a table-resolved type.
  • #[cgp_type] — defines the abstract-type component and generates the WithProvider impl.
  • HasType / TypeProvider — the built-in abstract-type component this binds.

The ideas behind it:

  • Abstract types — the associated types a context chooses for itself, which this alias supplies.

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.