Upgrade to Pro — share decks privately, control downloads, hide ads and more …

入門Go言語仕様 Underlying Type / Go Language Underlying Type

DQNEO
February 18, 2021

入門Go言語仕様 Underlying Type / Go Language Underlying Type

Go言語の “Underlying Type” の仕組みをわかりやすく解説します。
Go言語の設計原則や歴史にもせまります。

#gospecreading

DQNEO

February 18, 2021
Tweet

More Decks by DQNEO

Other Decks in Programming

Transcript

  1. 入門Go言語仕様輪読会
    “Underlying Type”
    @DQNEO
    2021-02-18 20:10

    View Slide

  2. 自己紹介
    ● @DQNEO (ドキュネオ)
    ● Goコンパイラ babygo の作者です
    ○ https://github.com/DQNEO/babygo
    ● 公式Goコンパイラ コントリビュート歴有り
    ● Go言語仕様書 コントリビュート歴有り(←New)

    View Slide

  3. 全ての型には
    “Underlying Type” がある

    View Slide

  4. underlyingの意味
    lying or situated under something
    --- Oxford’s Languages

    View Slide

  5. underlying type = 背後霊
    私 霊

    View Slide

  6. “Underlying Type”がなぜ重要なのか
    ● Assignability (代入可能性) の条件に使われている
    ● Generics で重要な役割をになう
    ○ 設計ドラフトで “underlying type” が11回登場
    https://go.googlesource.com/proposal/+/refs/heads/
    master/design/go2draft-type-parameters.md

    View Slide


  7. type MyInt int
    MyInt の underlying type は int

    View Slide

  8. イメージ
    背後霊
    MyInt
    int
    type MyInt int

    View Slide

  9. 全ての型には “Underlying Type” がある
    = 全ての型には 背後霊型がある

    View Slide

  10. MyInt
    int
    では int の underlying type は?
    type MyInt int
    ?

    View Slide

  11. int
    int
    int の underlying type は int

    View Slide

  12. ルール1:
    predeclared な boolean, numeric, string の underlyng type は
    自分自身
    ルール2:
    型リテラルの underlyng type は自分自身
    ルール3:
    型定義すると、左側と右側で underlyng type を共有する
    3つのルール

    View Slide

  13. predeclaredな
    boolean,string,numeric
    のunderlying typeは自分自身
    ルール1
    ● bool
    ● string
    ● int int8 int16 int32 int64 uint uint8 uint16
    uint32 uint64 uintptr complex64
    complex128 float32 float64

    View Slide

  14. 複合的な型
    type User struct {
    id int
    name string
    }
    User
    struct {
    id int
    name string
    }
    背後霊

    View Slide

  15. struct{...}の
    underlying type は?
    struct {
    id int
    name string
    }

    View Slide

  16. struct {
    id int
    name string
    }
    struct {
    id int
    name string
    }
    struct{...}の
    underlying typeは自分自身

    View Slide

  17. 型リテラルの underlying typeは
    自分自身
    ルール2
    例:
    *int
    struct{...}
    []string
    map[int]bool
    interface{...}
    ※前半発表のNobishiiさん
    の資料もご参照ください。

    View Slide

  18. type MyInt int
    MyInt
    int
    派生型から派生させると?
    type MyMyInt MyInt
    MyMyInt
    誰?

    View Slide

  19. int
    type MyInt int
    MyInt
    int
    派生型から派生させると?
    type MyMyInt MyInt
    MyMyInt
    えっ?

    View Slide

  20. 型定義をすると、
    左側と右側が underlying typeを共有する
    ルール3
    type B A
    左 右
    ※前半発表のNobishiiさんの資料も
    ご参照ください

    View Slide

  21. 背後霊を共有する
    type B A
    B A
    Aの背後霊

    View Slide

  22. type MyInt int
    MyInt int
    int
    背後霊を共有する

    View Slide

  23. type MyMyInt MyInt
    MyMyInt MyInt
    int
    背後霊を共有する

    View Slide

  24. type User struct{
    id…
    }
    User struct{id...}
    struct{id...}
    背後霊を共有する

    View Slide

  25. type AdminUser User
    AdminUser User
    struct{id...}
    背後霊を共有する

    View Slide

  26. type MyInt int
    type MyMyInt MyInt
    type MyMyMyInt MyMyInt
    MyInt int
    int
    フラットな組織
    MyMyInt
    MyMyMyInt

    View Slide

  27. func (x MyInt) method() {
    }
    MyInt int
    int
    Method の継承
    MyMyInt
    MyMyMyInt
    method なし
    method なし
    method なし method あり
    method なし

    View Slide

  28. Method の継承
    A defined type may have methods associated with it. It does not inherit any
    methods bound to the given type, but the method set of an interface type or
    of elements of a composite type remains unchanged:
    ● interface型 : 継承される
    ● 構造体型の要素のメソッド: 継承される
    ● それ以外の型: 継承されない

    View Slide

  29. 型のヒエラルキー
    (段階的階層)がない

    View Slide

  30. Go言語誕生の背景
    https://talks.golang.org/2009/go_talk-20091030.pdf

    View Slide

  31. https://talks.golang.org/2009/go_talk-20091030.pdf
    Go言語の設計原則

    View Slide

  32. ルール1:
    predeclared boolean, numeric, string の underlyng type は自分自身
    ルール2:
    型リテラルの underlyng type は自分自身
    ルール3:
    型定義すると、左側と右側で underlyng type を共有する
    まとめ

    View Slide

  33. View Slide

  34. クイズ1
    type (
    B1 string
    B2 B1
    )
    string, B1, B2 の それぞれの
    underlying type は?
    → 答えは言語仕様書に

    View Slide

  35. クイズ2
    type (
    B1 string
    B2 B1
    B3 []B1
    B4 B3
    )
    [ ]B1, B3, B4 のそれぞれの
    underlying type は?
    → 答えは言語仕様書に

    View Slide

  36. ご清聴
    ありがとうございました

    View Slide