AgentSkillsCN

cairo-generics-traits

当请求涉及在 Cairo 中使用泛型参数或泛型签名时,讲解 Cairo 的泛型数据类型与泛型函数、结构体、枚举、方法以及 impl 的语法。

SKILL.md
--- frontmatter
name: cairo-generics-traits
description: Explain Cairo generics and traits at a high level, including monomorphization and code size impact; use when a request asks why generics/traits reduce duplication, how trait bounds relate to generics, or why contract size increases with generics.

Cairo Generics and Traits Overview

Overview

Provide conceptual guidance on generics and traits, including when to use them and tradeoffs like monomorphization.

Quick Use

  • Read references/generics-traits.md before answering.
  • Use simple examples (like Option<T>) to explain placeholders for types.
  • Call out monomorphization when users ask about binary or contract size.

Response Checklist

  • Identify duplicated logic that generics can remove.
  • Explain that each concrete type produces a specialized implementation.
  • Mention traits as behavior constraints for generic types.
  • For generic impls, remind users to add required trait bounds (+Drop<T>, +Copy<T>).
  • For generic structs, remind users to add #[derive(Drop, Copy)].

Common Pitfalls

  • Missing trait bounds: Generic impls need +Drop<T> and often +Copy<T> in the impl signature.
  • Missing derives: Generic structs need #[derive(Drop, Copy)] to work with the trait bounds.
  • Syntax: Use impl Foo<T, +Drop<T>, +Copy<T>> of MyTrait<T> (note the + prefix).

Example Requests

  • "Why did my contract get bigger after adding generics?"
  • "What do generics and traits buy me in Cairo?"
  • "How do trait bounds relate to generics?"
  • "My generic implementation won't compile"

Cairo by Example