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

Go Generics Overview

karupanerura
December 22, 2022

Go Generics Overview

karupanerura

December 22, 2022
Tweet

More Decks by karupanerura

Other Decks in Technology

Transcript

  1. • Software Engineer @ DeNA • Perl / Go /

    Java / Type Script • AWS / Google Cloud • Japan Perl Association ୅ද • Shibuya.pm Ϧʔμʔ @karupanerura
  2. Generics • ܕ(Type)ͷҰछɺ૯শܕ • ʮҰఆͷ੍໿Λຬͨ͢ܕʯΛ෦෼తʹ࣋ͭܕΛදݱ͢Δ • e.g.) type Array[T any]

    []T // TΛཁૉͱͯ࣋ͭ͠഑ྻ • e.g.) func identity[T any](v T) T { return v } // Tܕͷ஋Λड͚औͬͯฦؔ͢਺ • ܕͷදݱͷࣗ༝౓্͕͕Δ
  3. Genericsͷ࣮ݱํ๏ͷྑ͠ѱ͠ • શల։ • ΞηϯϒϦϨϕϧͰ͸௨ৗͷؔ਺ͱมΘΒͳ͍ͷͰύϑΥʔϚϯε͕ྑ͍ • Ͳ͏ͯ͠΋ϓϩάϥϜαΠζ͕૿͑Δ • ܕফڈ •

    ίϯύΠϧ͞ΕΔϓϩάϥϜαΠζΛখ͘͞Ͱ͖Δ • ܕม਺ʹॊೈͳ੍໿ֻ͕͚ΒΕΔʢίϯύΠϧ࣌ʹෆఆͳ੍໿΋Մೳʣ • ܕ৘ใΛ࣮ߦ࣌ʹ࢒͢࢓૊Έʹͳ͍ͬͯͳ͍ͱϦϑϨΫγϣϯ͕Ͱ͖ͳ͍
  4. Go Genericsͷ࢓૊Έ • جຊతʹ͸શల։ͬΆ͍ৼΔ෣͍Λ͢Δ • શల։ͬΆ͍ײ͡ͷ࢓૊ΈͳͷͰrelfectionͷڍಈ΋௚ײత • e.g.) https://go.dev/play/p/z5Sv88yHAqs •

    GC Shape Stenciling: GC͔Βݟ͑Δܕ͝ͱʹల։͢Δ • ಉҰͷGC Shapeͷ৔߹ͷৼΔ෣͍ͷҧ͍͸ܕ৘ใΛ࣮ߦ࣌ʹࢀরͯ͠ղܾ • ຆͲͷ৔߹͸࣮ࡍͷܕ͝ͱʹผʑͷؔ਺ʹల։͞ΕΔ • refs. https://go.googlesource.com/proposal/+/refs/heads/master/design/generics-implementation-gcshape.md
  5. Go Genericsͷจ๏ • ܕ • ܕఆٛ: type GenericType[TypeParameter Constraint] ...

    • ؔ਺ఆٛ: func GeneicFunc[TypeParameter Constraint](...) ... { ... } • ར༻ • ม਺ఆٛ: var v GenericType[string] • ؔ਺ݺग़: GenericFunc[int](1)
  6. Constraint • ܕύϥϝʔλͱͯ͠ड͚෇͚Δܕ͕ຬͨ͢΂੍͖໿ • JavaͰ͍͏ͱ͜Ζͷ<T extends List>ͱ͔͢ΔΠϝʔδ • X[T any]

    ͱ΍ͬͯͨͱ͜Ζͷany͕ͦΕ • ==, != ͕Ͱ͖ΔܕͰ͋Δ͜ͱΛࣔ͢ comparable ͱ͍͏Constraint΋͋Δ • interfaceܕͱͯ͠දݱ͢Δ • e.g.) type MyConstraintA interface { int64 | int32 } • e.g.) type MyConstraintB interface { ~int64 }
  7. Constraint • ܕύϥϝʔλͱͯ͠ड͚෇͚Δܕ͕ຬͨ͢΂੍͖໿ • JavaͰ͍͏ͱ͜Ζͷ<T extends List>ͱ͔͢ΔΠϝʔδ • X[T any]

    ͱ΍ͬͯͨͱ͜Ζͷany͕ͦΕ • ==, != ͕Ͱ͖ΔܕͰ͋Δ͜ͱΛࣔ͢ comparable ͱ͍͏Constraint΋͋Δ • interfaceܕͱͯ͠දݱ͢Δ • e.g.) type MyConstraintA interface { int64 | int32 } • e.g.) type MyConstraintB interface { ~int64 } ͳʹ͜Ε
  8. Underlyng Typeͬ͘͟Γղઆ • ͢΂ͯͷܕ͕҉໧తʹ࣋ͭ֓೦ • ୅ೖՄೳੑɺܕಉҰੑͳͲΛ൑ఆ͢Δ৚݅ʹͳͬͯ͘Δܕ • ྫ • int64ͷunderlyng

    type͸int64 • type X int64ͱͨ͠ͱ͖ͷunderlyng type͸int64 • type Y Xͱͨ͠ͱ͖ͷunderlyng type΋int64 • ৄ͍͜͠ͱ͸ઌͷZennͷهࣄ΍SpeakerDeckͷεϥΠυΛಡΜͰ
  9. Underlyng Type Λ࢖ͬͯԿ͕ग़དྷΔ͔ • ͦͷܕ͕ͲͷΑ͏ͳੑ࣭Λ࣋ͭͷ͔͕෼͔Δ • ͨͱ͑͹ ~int64 ͸Ճݮࢉ΍ൺֱԋࢉͳͲ͕Ͱ͖Δ •

    e.g.) ܕม਺X͕~int64Λຬͨ͢ͳΒXܕͷ஋x,yʹରͯ͠ x > y ͕Ͱ͖Δ • ಉ༷ʹXܕͷ஋x͸~int64Λຬͨ͢ଞͷܕ͔Β҆શʹܕม׵Ͱ͖Δ • OrderedͳͲศརͳ΍ͭΛ·ͱΊͨ४ඪ४ύοέʔδ(experimental)΋͋Δ • golang.org/x/exp/constraints
  10. Go Genericsͷจ๏ • ܕ • ܕఆٛ: type GenericType[TypeParameter Constraint] ...

    • ؔ਺ఆٛ: func GeneicFunc[TypeParameter Constraint](...) ... { ... } • ར༻ • ม਺ఆٛ: var v GenericType[string] • ؔ਺ݺग़: GenericFunc[int](1)
  11. Go Genericsͷจ๏ͷ۩ମྫ • ܕ • ܕఆٛ: type MyComparableArray[T comparable] []T

    • ؔ਺ఆٛ: func MyIncrement[T ~int64](v T) T { return v + 1 } • ར༻ • ม਺ఆٛ: x := MyComparableArray[string]{"a", "b", "c"} • ؔ਺ݺग़: MySort([]int{3, 2, 1})
  12. Go GenericsͰग़དྷΔ͜ͱ • ܕͱؔ਺ʹܕม਺Λ෇༩Ͱ͖Δ • ෳ਺෇༩͢Δ͜ͱ΋Ͱ͖Δ • ConstraintͰܕม਺Λ੍໿Ͱ͖Δ • ConstraintͰ͸Underlyng

    TypeΛ࢖ͬͨࢦఆ΋Մೳ • ಉҰͷUnderlyng TypeΛ࣋ͭܕͷ஋Λ୅ೖ͢Δ͜ͱ͕Ͱ͖Δ • ൺֱԋࢉࢠ΍ࢉज़ԋࢉࢠΛ࢖͏͜ͱ͕Ͱ͖Δ
  13. Go GenericsͰग़དྷͳ͍͜ͱ • ܕม਺ͷᐆດԽ • ͨͱ͑͹ɺJavaͰ͍͏ͱ͜Ζͷ Y<? extends X> Έ͍ͨͳ͜ͱ͸Ͱ͖ͳ͍

    • ෦෼ಛघԽ • ͨͱ͑͹ɺܕ͕intͰ͋Δͱ͖͸͜ͷఆٛΛ࢖͏Έ͍ͨͳ͜ͱ͸Ͱ͖ͳ͍ • ܕม਺ΛՄม௕Ͱड͚෇͚Δ • ͨͱ͑͹ɺC++Ͱ͍͏ͱ͜Ζͷ template<class V...> ͸Ͱ͖ͳ͍
  14. Go GenericsͰग़དྷͳ͍͜ͱ • ܕม਺Λ࢖ͬͨΦʔόʔϩʔυ • Φʔόʔϩʔυ͕ͦ΋ͦ΋Ͱ͖ͳ͍ • ܕม਺ͷܭࢉ • ͨͱ͑͹ɺC++

    TemplateͰ͍͏ͱ͜Ζͷ typ<T-1> ͸Ͱ͖ͳ͍ • ఆ਺Λܕม਺ʹ͍ΕΔ • ͨͱ͑͹ɺC++ TemplateͰ͍͏ͱ͜Ζͷ typ<1> ͸Ͱ͖ͳ͍ • C++Ͱ͍͏ͱ͜ΖͷTemplate Meta ProgrammingΈ͍ͨͳ͜ͱ
  15. Go GenericsͰग़དྷͳ͍͜ͱ • ϝιουͰܕม਺Λ࣋ͭ͜ͱ͸Ͱ͖ͳ͍ • e.g.) func (x *X) foo[Y

    any](...) ... {...} // ίϯύΠϧΤϥʔ • e.g.) func (x *X[Y]) foo(...) ... {...} // ͜Ε͸OK • ܕม਺Λ࣋ͭؔ਺ͷͳ͔ͰܕΛఆٛ͢Δ͜ͱ͸Ͱ͖ͳ͍ • e.g.) func foo[T any](v T) { type empty struct{} } // ίϯύΠϧΤϥʔ • ͦͷ΄͔ࡉʑͱͨ͠Ͱ͖ͳ͍͜ͱ͸ଟʑ
  16. ར༻ྫ • ४ඪ४ܥ • golang.org/x/exp/slices • golang.org/x/exp/maps • OSS •

    github.com/samber/lo • github.com/Code-Hex/go-generics-cache • github.com/moznion/go-optional