User-Defined Types

Beyond built-in types, Compact allows you to define custom types to model your domain. This chapter covers the ways to create user-defined types:

  • Structs - Custom record types with named fields
  • Enums - Named enumeration types with distinct variants
  • Generic Types - Parameterized types for reusable abstractions

Overview

DeclarationPurposeExample
struct Name { ... }Record with named fieldsstruct Point { x: Field, y: Field }
enum Name { ... }Enumeration with variantsenum Status { pending, approved }

Key Concepts

Structs

Structs group related data into a single type with named fields:

struct Player {
    address: Bytes<32>,
    score: Uint<64>,
    isActive: Boolean
}

Access fields with dot notation: player.score

Source: User-defined types

Enums

Enums define a type with a fixed set of named values:

enum GameState { waiting, playing, finished }

Access variants with dot notation: GameState.waiting

⚠️ Important: Use dot notation (GameState.waiting), not Rust-style GameState::waiting.

Source: User-defined types

Exporting Types

To use custom types from TypeScript, export them:

export struct Player { ... }
export enum GameState { ... }

Source: Top-level exports

When to Use Each

Use CaseRecommended Type
Group related fieldsstruct
Finite set of statesenum
Reusable containerGeneric struct

What’s Next

  • Structs - Creating and using struct types
  • Enums - Defining enumeration types
  • Generic Types - Parameterized type definitions