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. 自己紹介 • @DQNEO (ドキュネオ) • Goコンパイラ babygo の作者です ◦ https://github.com/DQNEO/babygo

    • 公式Goコンパイラ コントリビュート歴有り • Go言語仕様書 コントリビュート歴有り(←New)
  2. “Underlying Type”がなぜ重要なのか • Assignability (代入可能性) の条件に使われている • Generics で重要な役割をになう ◦

    設計ドラフトで “underlying type” が11回登場 https://go.googlesource.com/proposal/+/refs/heads/ master/design/go2draft-type-parameters.md
  3. ルール1: predeclared な boolean, numeric, string の underlyng type は

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

    int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr complex64 complex128 float32 float64
  5. 複合的な型 type User struct { id int name string }

    User struct { id int name string } 背後霊
  6. struct { id int name string } struct { id

    int name string } struct{...}の underlying typeは自分自身
  7. 型リテラルの underlying typeは 自分自身 ルール2 例: *int struct{...} []string map[int]bool

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

    ※前半発表のNobishiiさんの資料も ご参照ください
  9. type MyInt int type MyMyInt MyInt type MyMyMyInt MyMyInt MyInt

    int int フラットな組織 MyMyInt MyMyMyInt
  10. func (x MyInt) method() { } MyInt int int Method

    の継承 MyMyInt MyMyMyInt method なし method なし method なし method あり method なし
  11. 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型 : 継承される • 構造体型の要素のメソッド: 継承される • それ以外の型: 継承されない
  12. ルール1: predeclared boolean, numeric, string の underlyng type は自分自身 ルール2:

    型リテラルの underlyng type は自分自身 ルール3: 型定義すると、左側と右側で underlyng type を共有する まとめ
  13. クイズ1 type ( B1 string B2 B1 ) string, B1,

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

    B3 ) [ ]B1, B3, B4 のそれぞれの underlying type は? → 答えは言語仕様書に