Files
2nd/10_Wiki/Topics/AI_and_ML/Exhaustiveness-Checking.md
T
koriweb d8a80f6272 chore(wiki): dangling 링크 canonical 정규화 (768파일/1200건)
이름만 다른(표기 변형) [[위키링크]]를 대상 문서의 canonical 제목으로 치환해
끊겼던 1,200개 링크를 연결. 제목/파일명 정규화 일치만 적용하고 별칭 매칭은
과병합 위험으로 제외(애매성 가드). 원본은 _link_reconcile_backup/ 에 백업.
도구: Datacollect/scripts/link_reconcile_apply.mjs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-08 12:24:15 +09:00

6.3 KiB

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-exhaustiveness-checking Exhaustiveness Checking 10_Wiki/Topics verified self
exhaustive match
never type
sealed types
tagged union
switch exhaustive
none A 0.95 applied
type-system
typescript
rust
exhaustive
never-type
discriminated-union
2026-05-10 pending
language
TypeScript / Rust / Kotlin

Exhaustiveness Checking

매 한 줄

"매 type-level 의 매 case 의 covered 의 verify". 매 switch / match 의 missing branch 의 compile-time 의 catch. 매 TypeScript never, 매 Rust match, 매 Kotlin sealed. 매 modern: 매 type-driven design 의 핵심.

매 핵심

매 mechanism

  • Discriminated union + 매 switch.
  • Sealed types: 매 known set.
  • never type (TS): 매 unreachable.
  • Pattern match (Rust, Scala, Kotlin).

매 응용

  1. State machine: 매 모든 state 의 handle.
  2. Event handler: 매 모든 event.
  3. Error type: 매 union of errors.
  4. API response.
  5. Theme / variant.

💻 패턴

TypeScript (never)

type Shape =
  | { kind: 'circle'; radius: number }
  | { kind: 'square'; side: number }
  | { kind: 'rectangle'; w: number; h: number };

function area(s: Shape): number {
  switch (s.kind) {
    case 'circle': return Math.PI * s.radius ** 2;
    case 'square': return s.side ** 2;
    case 'rectangle': return s.w * s.h;
    default: {
      const _exhaustive: never = s;  // 매 compile error if Shape grows
      throw new Error(`Unhandled: ${_exhaustive}`);
    }
  }
}

assertNever helper

function assertNever(x: never): never {
  throw new Error(`Unexpected: ${JSON.stringify(x)}`);
}

function handle(s: Shape) {
  switch (s.kind) {
    case 'circle': return ...;
    case 'square': return ...;
    case 'rectangle': return ...;
    default: return assertNever(s);
  }
}

Rust (match)

enum Shape {
    Circle { radius: f64 },
    Square { side: f64 },
    Rectangle { w: f64, h: f64 },
}

fn area(s: &Shape) -> f64 {
    match s {
        Shape::Circle { radius } => std::f64::consts::PI * radius * radius,
        Shape::Square { side } => side * side,
        Shape::Rectangle { w, h } => w * h,
        // 매 missing arm = compile error
    }
}

Kotlin sealed class

sealed class Shape {
    data class Circle(val r: Double) : Shape()
    data class Square(val side: Double) : Shape()
}

fun area(s: Shape) = when (s) {
    is Shape.Circle -> Math.PI * s.r * s.r
    is Shape.Square -> s.side * s.side
    // 매 exhaustive — 매 compiler enforces
}

State machine (TS)

type AppState =
  | { status: 'idle' }
  | { status: 'loading'; startedAt: Date }
  | { status: 'success'; data: Data }
  | { status: 'error'; message: string };

function render(s: AppState) {
  switch (s.status) {
    case 'idle': return <Welcome />;
    case 'loading': return <Spinner />;
    case 'success': return <Display data={s.data} />;
    case 'error': return <Error msg={s.message} />;
    default: return assertNever(s);
  }
}

Result type

type Result<T, E> = { tag: 'ok'; value: T } | { tag: 'err'; error: E };

function handle<T, E>(r: Result<T, E>): T {
  switch (r.tag) {
    case 'ok': return r.value;
    case 'err': throw r.error;
    default: return assertNever(r);
  }
}

Reducer (Redux)

type Action =
  | { type: 'add'; value: number }
  | { type: 'sub'; value: number }
  | { type: 'reset' };

function reducer(state: number, action: Action): number {
  switch (action.type) {
    case 'add': return state + action.value;
    case 'sub': return state - action.value;
    case 'reset': return 0;
    default: return assertNever(action);
  }
}

Type predicate (narrow)

function isCircle(s: Shape): s is Extract<Shape, { kind: 'circle' }> {
  return s.kind === 'circle';
}

TS 4.4+ template literal (string union)

type HttpStatus = 200 | 404 | 500;
function describe(s: HttpStatus): string {
  switch (s) {
    case 200: return 'OK';
    case 404: return 'Not Found';
    case 500: return 'Server Error';
    default: return assertNever(s);
  }
}

Compile fail demo

type Theme = 'light' | 'dark' | 'sepia';
function bg(t: Theme): string {
  switch (t) {
    case 'light': return '#fff';
    case 'dark': return '#000';
    // 매 missing 'sepia'
    default: const _: never = t;  // ❌ Type 'sepia' is not assignable to 'never'
      return '';
  }
}

ESLint rule

// .eslintrc
{
  rules: {
    '@typescript-eslint/switch-exhaustiveness-check': 'error',
  }
}

Rust if-let / match guard

match (state, action) {
    (State::Idle, Action::Start) => State::Running,
    (State::Running, Action::Stop) => State::Idle,
    (s, _) => s,  // 매 catchall — 매 explicit 의 careful
}

매 결정 기준

상황 Approach
TS state machine Discriminated union + never
Rust enum match (built-in)
Kotlin sealed + when
Redux tagged action + assertNever
API variant Discriminated union
Throwaway script Skip

기본값: 매 TS = discriminated union + assertNever + ESLint rule. 매 Rust = match. 매 Kotlin = sealed when.

🔗 Graph

🤖 LLM 활용

언제: 매 type-rich domain. 매 state machine. 매 reducer. 언제 X: 매 dynamic / heterogeneous.

안티패턴

  • Catchall default: 매 exhaustiveness 의 lose.
  • String enum 의 boolean check: 매 brittle.
  • No assertNever: 매 default 의 silent.
  • Mix kind tag: 매 narrow 의 fail.
  • Lint disabled: 매 enforce X.

🧪 검증 / 중복

  • Verified (TS handbook, Rust book, Effective TypeScript).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — never + 매 TS / Rust / Kotlin / Redux exhaustive code