改めて語ろう、Goにおける直和型の実践とその課題 / hatena.go #3

utagawa kiki
November 26, 2026

改めて語ろう、Goにおける直和型の実践とその課題 / hatena.go #3

utagawa kiki

November 26, 2026

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

67 slides
  1. 直和型の例 (1) • 信号機の色 = 赤 | 黄 | 青

    ◦ 列挙型 (enum) • 図形 = 円(半径) | 長方形(縦, 横) | 三角形(底辺, 高さ) ◦ 面積を計算するためのプロパティを持つ例 (union) 4
  2. 直和型の例 (2) • Option = Some(T) | None ◦ 値があるかもしれない

    (ないかもしれない) 型 • Result = Ok(T) | Err(R) ◦ 成功もしくは失敗の型 5
  3. ケーススタディ • 列挙型 (enum) • union • optionalな値 • エラー

    • 状態遷移するモデルの型 • データストア上の表現 14
  4. 列挙型 (2) type T int const ( T1 T =

    iota T2 T3 ) type T string const ( T1 T = "t1" T2 T = "t2" T3 T = "t3" ) 16
  5. 列挙型 (3) func F(t T) { switch t { case

    T1: case T2: case T3: } } 17
  6. sealed interfaceによるunion (1) U = U1 | U2 package pkg

    type U1 struct{} func (*U1) isU() {} type U2 struct{} func (*U2) isU() {} 22
  7. sealed interfaceによるunion (2) func F(u U) { switch u :=

    u.(type) { case *U1: case *U2: } } 23
  8. optionalな値 (3) • プリミティブでない型 ◦ ◦ 「値がある」「値がない」 のunion型を都度定義 使う側でtype switch

    type A interface { isA() } type ANone struct{} type ASome struct { Field1 int Field2 string } func (*ANone) isA() {} func (*ASome) isA() {} 27
  9. optionalな値 (4) • プリミティブでない型 ◦ 「値がある」「値がない」 の直和型を都度定義 • interfaceにgetterメソッ ドを足す場合もある

    ◦ (T, bool) を返すとGo の慣習に合わせやすい type A interface { isA() GetField1() (int, bool) GetField2() (string, bool) } 28
  10. Option型を導入するかどうか • Option = Some(T) | None のようなユーティ リティー型を定義して使う? •

    今は使っていない ◦ スモールスタートを重視 ◦ Goらしいコードからのジャンプを小さくする 29
  11. エラーハンドリング • Goのエラーハンドリングの慣習に倣う ◦ エラーをerror型で返す ◦ if err != nil

    で分岐 ◦ errors.Is / errors.AsType でエラーの種類に応じて分岐する • 独自のResult型は導入しない 30
  12. (T, error) は直和型ではない • 実際には他の組み合わせもあり得る ◦ (nil, nil) ▪ 正常系として「値がない」ことを表す

    ◦ (有効な値, error) ▪ 部分的な成功 (失敗) の表現 ▪ io.Reader.Read は (0より大きい整数, io.EOF) を返しうる 32
  13. (T, error) を直和型にしたい (2) • return nil, nil はlinterで検出できる •

    (nil, nil) や (有効な値, error) を返しうる関数 の検出は自明ではない ◦ なんかアノテーションできるとよい? (未解決) 34
  14. 状態遷移するモデルの型 (2) • sealed interface • 共通フィールド ◦ interfaceのメソッド としてgetterを定義

    ◦ 共通フィールドの型 をembed type T interface { isT() GetCommon1() int GetCommon2() int } type TCommon struct { Common1 int Common2 int } func (*TCommon) isT() {} func (t *TCommon) GetCommon1() int { ... } 36
  15. 状態遷移するモデルの型 (3) • sealed interface • 共通フィールド ◦ interfaceのメソッド としてgetterを定義

    ◦ 共通フィールドの型 をembed type T1 struct { *TCommon } type T2 struct { *TCommon Field1 int } type T3 struct { *TCommon Field1 int Field2 int } 37
  16. Option型を導入するかどうか (1) • Option = Some(T) | None のようなユーティ リティー型を定義して使う?

    • 今は使っていない ◦ スモールスタートを重視 ◦ Goらしいコードからのジャンプを小さくする 46
  17. Option型を導入するかどうか (3) type Option[T any] struct { value T hasValue

    bool } func (*Option[T]) Map[U any](f ...) *Option[U] { ... } 48
  18. 直積を直和として扱う (3) • 採用事例はある ◦ kubernetesのVolumeSourceとか • 実際どうなのか ◦ structのメンバを増やすだけでよい

    ◦ シリアライズ・デシリアライズが簡単 ◦ 型安全性とのトレードオフが気になる 51
  19. 共通フィールドの表現方法 (2) type TState interface { isTState() } type T[S

    TState] struct { Common1 int Common2 int State S } type T1 struct{} type T2 struct { Field1 int } type T3 struct { Field1 int Field2 int } 53
  20. 共通フィールドの表現方法 (3) • sealed interfaceで簡単に実現できていたこ とが困難になっていないか (未精査) ◦ 結局sealed interfaceは要る

    ◦ interfaceのメソッドをジェネリクスにしたくなった ときに詰む? ◦ うまくいった事例があれば教えてください 54
  21. ジェネリクス以降の潮流 (2) • proposal: spec: sum types based on general

    interfaces #57644 ◦ interface { X | Y } の | を型パラメータ以外でも使え るようにする ◦ 2023/1/6 に起票 ▪ 2026/11/17 現在 Open 62
  22. ジェネリクス以降の潮流 (3) • proposal: spec: define sum types as an

    extension of interfaces #80607 ◦ sum_interfaceというキーワードでsealed interface を直和型として定義できるようにする ◦ likely declineでcloseされた ▪ 網羅性チェックがあると、直和型のメンバーを追加すること が非互換変更になる 63
  23. 参考 • pkg.go.dev ◦ https://pkg.go.dev/io#Reader • 単一テーブル継承(STI)/クラステーブル継承(CTI)/具象クラス継 承(CCI)について整理する https://zenn.dev/manase/articles/7b8af72db3a3ab •

    Frequently Asked Questions (FAQ) - The Go Programming Language https://go.dev/doc/faq#variant_types • FAQ/blog: lack of union types · Issue #1935 · golang/go https://github.com/golang/go/issues/1935 67

More Decks by utagawa kiki

Other Decks in Programming