generics-implementation
Expert skill for implementing parametric polymorphism including type parameter bounds, monomorphization, type erasure, variance, higher-kinded types, and associated types.
What this skill does
# Generics Implementation Skill
Implement parametric polymorphism for programming languages including generics, type bounds, and compilation strategies.
## Capabilities
- Design generic syntax and type parameter bounds
- Implement monomorphization (Rust-style)
- Implement type erasure (Java-style)
- Handle variance in generic types
- Implement higher-kinded types (if applicable)
- Design trait/interface bounds
- Handle associated types
- Implement generic method dispatch
## Usage
Invoke this skill when you need to:
- Add generics to a language
- Implement monomorphization or type erasure
- Design trait bounds and constraints
- Handle variance and subtyping with generics
## Inputs
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| compilationStrategy | string | Yes | Strategy (monomorphization, erasure, dictionary) |
| features | array | No | Features to implement |
| varianceModel | string | No | Variance handling (explicit, inferred, none) |
| boundsSystem | object | No | Bounds system configuration |
### Compilation Strategies
```json
{
"compilationStrategy": "monomorphization", // Rust, C++
"compilationStrategy": "erasure", // Java, TypeScript
"compilationStrategy": "dictionary" // Haskell, Swift witness tables
}
```
### Feature Options
```json
{
"features": [
"type-parameters",
"trait-bounds",
"associated-types",
"variance",
"higher-kinded-types",
"default-type-parameters",
"const-generics",
"where-clauses",
"specialization"
]
}
```
## Output Structure
```
generics/
├── syntax/
│ ├── type-params.grammar # Type parameter syntax
│ ├── bounds.grammar # Bounds and constraints
│ └── where-clause.grammar # Where clause syntax
├── typing/
│ ├── generic-types.ts # Generic type representation
│ ├── bounds-checking.ts # Bounds verification
│ ├── variance.ts # Variance checking
│ └── instantiation.ts # Type instantiation
├── compilation/
│ ├── monomorphization.ts # Monomorphization
│ ├── erasure.ts # Type erasure
│ └── dictionary.ts # Dictionary passing
├── inference/
│ ├── type-inference.ts # Generic type inference
│ └── constraint-solving.ts # Constraint resolution
└── tests/
├── bounds.test.ts
├── variance.test.ts
└── compilation.test.ts
```
## Generic Type System
### Type Parameter Syntax
```typescript
// Basic generics
struct Vec<T> {
data: T[],
len: usize
}
// Multiple type parameters
struct HashMap<K, V> {
buckets: Array<(K, V)>
}
// Type parameter bounds
fn sort<T: Ord>(arr: &mut [T]) { ... }
// Where clauses for complex bounds
fn process<T, U>(t: T, u: U) -> bool
where
T: Clone + Debug,
U: AsRef<T>
{ ... }
// Default type parameters
struct Container<T = i32> {
value: T
}
// Const generics
struct Array<T, const N: usize> {
data: [T; N]
}
```
### Generic Type Representation
```typescript
interface GenericType {
name: string;
typeParams: TypeParameter[];
body: Type;
}
interface TypeParameter {
name: string;
bounds: TypeBound[];
variance: Variance;
default?: Type;
}
interface TypeBound {
trait: TraitRef;
// Additional constraints
}
type Variance = 'covariant' | 'contravariant' | 'invariant' | 'bivariant';
// Type application
interface TypeApplication {
generic: GenericType;
args: Type[];
}
```
## Monomorphization
```typescript
// Monomorphization: generate specialized code for each type instantiation
interface MonomorphizationContext {
instantiations: Map<string, Type[]>[]; // Track all instantiations
generatedCode: Map<string, GeneratedFunction>;
}
function monomorphize(
program: Program,
entryPoints: FunctionRef[]
): MonomorphizedProgram {
const ctx: MonomorphizationContext = {
instantiations: [],
generatedCode: new Map()
};
// Collect all instantiations starting from entry points
for (const entry of entryPoints) {
collectInstantiations(entry, ctx);
}
// Generate specialized code for each instantiation
for (const [signature, typeArgs] of ctx.instantiations) {
const original = lookupGenericFunction(signature);
const specialized = specializeFunction(original, typeArgs);
ctx.generatedCode.set(mangleName(signature, typeArgs), specialized);
}
return buildMonomorphizedProgram(ctx);
}
function specializeFunction(
fn: GenericFunction,
typeArgs: Type[]
): SpecializedFunction {
// Substitute type parameters with concrete types
const substitution = buildSubstitution(fn.typeParams, typeArgs);
return {
name: mangleName(fn.name, typeArgs),
params: fn.params.map(p => substituteType(p.type, substitution)),
returnType: substituteType(fn.returnType, substitution),
body: substituteInBody(fn.body, substitution)
};
}
// Name mangling for monomorphized functions
function mangleName(baseName: string, typeArgs: Type[]): string {
return `${baseName}_${typeArgs.map(typeToString).join('_')}`;
}
```
## Type Erasure
```typescript
// Type erasure: erase generic types at runtime, use casts
function eraseGenericType(type: Type): Type {
if (type.kind === 'typeParam') {
// Erase to bound (or Object if unbounded)
return type.bounds.length > 0
? type.bounds[0] // Erase to first bound
: ObjectType;
}
if (type.kind === 'application') {
// Erase type arguments
return eraseGenericType(type.generic);
}
if (type.kind === 'generic') {
// Erase body
return eraseGenericType(type.body);
}
return type;
}
// Insert casts at usage sites
function insertCasts(expr: Expr, expectedType: Type, actualType: Type): Expr {
const erasedExpected = eraseGenericType(expectedType);
const erasedActual = eraseGenericType(actualType);
if (!typesEqual(erasedExpected, erasedActual)) {
return {
type: 'cast',
expr: expr,
targetType: erasedExpected
};
}
return expr;
}
```
## Variance
```typescript
// Variance checking
type Variance = 'covariant' | 'contravariant' | 'invariant' | 'bivariant';
interface VarianceChecker {
// Compute variance of type parameter in type
computeVariance(typeParam: TypeParameter, type: Type): Variance;
// Check if variance annotation is correct
checkVariance(generic: GenericType): VarianceError[];
// Infer variance from usage
inferVariance(generic: GenericType): Map<TypeParameter, Variance>;
}
function computeVariance(param: TypeParameter, type: Type): Variance {
switch (type.kind) {
case 'typeParam':
return type.name === param.name ? 'covariant' : 'bivariant';
case 'function':
// Contravariant in parameter types, covariant in return
const paramVariance = combineVariances(
type.params.map(p => flipVariance(computeVariance(param, p)))
);
const returnVariance = computeVariance(param, type.returnType);
return combineVariance(paramVariance, returnVariance);
case 'application':
// Combine based on declared variance of type constructor
return combineVariances(
type.args.map((arg, i) => {
const declaredVariance = type.generic.typeParams[i].variance;
const usageVariance = computeVariance(param, arg);
return multiplyVariance(declaredVariance, usageVariance);
})
);
case 'mutable':
// Mutable positions are invariant
return 'invariant';
default:
return 'bivariant';
}
}
// Variance rules for subtyping
function isSubtype(sub: Type, sup: Type): boolean {
if (sub.kind === 'application' && sup.kind === 'application') {
if (sub.generic !== sup.generic) return false;
return sub.args.every((subArg, i) => {
const supArg = sup.args[i];
const variance = sub.generic.typeParams[i].variance;
switch (variance) {
case 'covariant':
return isSubtype(subArg, supArg);
caRelated in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.