Skip to main content

WithDelegatedType

Resolve one or more abstract-type components from a shared table, through the WithProvider adapter.

Overview​

Under Context-Generic Programming (CGP), several abstract types can be resolved from one shared table, and WithDelegatedType<Components> is the alias WithProvider<UseDelegatedType<Components>>. It answers a context's abstract-type components by looking each type tag up in the Components table, on a context, the type that implements the trait, by adapting the foundational UseDelegatedType provider through the WithProvider layer. Like every CGP provider, it carries no runtime value.

Unlike WithType, this alias is not an alternative to a directly-wireable provider on a #[cgp_type] component: the bare UseDelegatedType supplies only the foundational TypeProvider, so WithDelegatedType is the form you wire there. The bare form serves only the built-in TypeProviderComponent, as its page shows.

Usage​

WithDelegatedType is not in the prelude. Import it from cgp::core::types. It takes one type parameter, the lookup table, and appears as the value of one or more abstract-type components' wiring entries:

use cgp::core::types::WithDelegatedType;

delegate_components! {
App {
[
ScalarTypeProviderComponent,
IndexTypeProviderComponent,
]: WithDelegatedType<AppTypes>,
}
}

The Components parameter is a type that maps each component marker to a concrete type, built as an ordinary delegate_components! table. The key is the marker because the WithProvider impl #[cgp_type] generates asks its inner provider for TypeProvider<Context, ScalarTypeProviderComponent>. A component the table has no entry for is unresolved, and the context does not implement that abstract type.

Examples​

A lookup table maps type tags to concrete types, and a context wires several type components through it:

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

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

#[cgp_type]
pub trait HasIndexType {
type Index;
}

pub struct App;
pub struct AppTypes;

delegate_components! {
AppTypes {
ScalarTypeProviderComponent: f64,
IndexTypeProviderComponent: usize,
}
}

delegate_components! {
App {
[
ScalarTypeProviderComponent,
IndexTypeProviderComponent,
]: WithDelegatedType<AppTypes>,
}
}

check_components! {
App {
ScalarTypeProviderComponent,
IndexTypeProviderComponent,
}
}

App routes both its scalar and index type components through WithDelegatedType<AppTypes>. For App's Scalar the provider finds f64 in AppTypes, and for Index it finds usize. One provider entry on App answers two abstract types, with the concrete choices held in one place where they can be reused, swapped, or supplied by a namespace.

When to use it​

Reach for WithDelegatedType when several abstract types should be answered from one shared table, so a context points its type components at the table rather than fixing each one at the wiring site.

Prefer WithType<T> or the plain UseType<T> for the common case of fixing one abstract type to one concrete type, which needs no table.

Under the hood​

WithDelegatedType<Components> is a type alias:

pub type WithDelegatedType<Components> = WithProvider<UseDelegatedType<Components>>;

The WithProvider adapter forwards a type component's provider-trait method to the inner provider's foundational method, and UseDelegatedType<Components> is the foundational TypeProvider that resolves each tag through a DelegateComponent lookup. The UseDelegatedType mechanism is documented on the UseDelegatedType page, and the generated WithProvider impl on the WithProvider page.

The ideas behind it:

  • Abstract types — the associated types a context chooses for itself, here from a shared table.

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.