Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introduction of the anonymous function syntax sugar into Go

Bd3590aaffe8948079d27795cb6f7388?s=47 yuroyoro
October 28, 2019

Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introduction of the anonymous function syntax sugar into Go

GoCon 2019 LT

Bd3590aaffe8948079d27795cb6f7388?s=128

yuroyoro

October 28, 2019
Tweet

Transcript

  1. Considering the introduction of the anonymous function syntax sugar into

    Go !ZVSPZPSP (Pʹແ໊ؔ਺ͷ౶ҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ
  2. (Pͷແ໊ؔ਺ var f = func(x int) int { return x

    * 2 }
  3. ͍ΘΏΔ"SSPX'VODUJPO͕΄͍͠ var f = (x int) int => { return

    x * 2 }
  4. ಈతܕ෇͖ͳݴޠͰ͸ʁ const f = (x) => { return x *

    2 } +BWB4DSJQU 1ZUIPO f = lambda x: x * 2 3VCZ f = ->(x) { x * 2 }
  5. ੩తܕ෇͖ͳݴޠͰ͸ʁ const f = (x:number): number => { return x

    * 2 } 5ZQF4DSJQU +BWB 4DBMB Function<Integer, Integer> f = (Integer x) -> { return x * 2; }; val f: (Int) => Int = (x) => { x * 2 }
  6. (PͰ͸͜ΜͳܗͷߏจʹͳΓͦ͏ (name type, …) type => { function body }

  7. ྫ͑͹͜Μͳ;͏ʹ (x int) int => { return x * 2

    }
  8. (PίϯύΠϥΛվ଄͠Α͏ʂ src/cmd/compile/ https://github.com/golang/go

  9. ίϯύΠϥʹ͸ͭͷϑΣʔζ͕͋Δ 1. Parsing 2. Type-checking and AST transformations 3. Generic

    SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE
  10. ౶ҥߏจͷ௥ՃͳͷͰɺ1BSTFSΛมߋ 1. Parsing 2. Type-checking and AST transformations 3. Generic

    SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE
  11. (Pͷߏจղੳ LL(1)

  12. ͍͍ͤͥͻͱͭઌͷτʔΫϯ͔͠ઌಡΈ͠ͳ͍ var f = ( x * 2 ) ↑

    current: ( : _Lparen next : x : _Name
  13. ͷ࣍ʹ໊લ͕དྷͨΒɺࣜ &YQS ʹܾ·Δ var f = ( x * 2

    ) ↑ current: ( : _Lparen next : x : _Name Expr
  14. "SSPX'VODUJPO͕ೖΔͱʜʜ var f = ( x int ) int =>

    { … } ↑ current: ( : _Lparen next : x : _Name
  15. ͷ࣍ʹ໊લ͕དྷͯ΋ɺࣜ &YQS ͔ؔ਺ 'VOD5ZQF ͔ Θ͔Βͳ͍ var f = (

    x int ) int => { … } ↑ current: ( : _Lparen next : x : _Name Expr or FuncType?
  16. Ͳ͏ʹ͔ͳΒͳ͍

  17. ߏจΛม͑ͯΈΑ͏ ->(name type, …) type { function body }

  18. 3VCZͬΆ͘ ͜ΕͳΒύʔεͰ͖Δͧ ->(x int) int => { return x *

    2 }
  19. AGVODAΛAAʹม͑Δ͚ͩ ->(x int) int => { return x * 2

    } func(x int) int => { return x * 2 } ↓
  20. ॻ͍ͯΈͨ src/cmd/compile/internal/syntax/parser.go | 2 +- src/cmd/compile/internal/syntax/scanner.go | 7 ++++++- src/cmd/compile/internal/syntax/token_string.go

    | 4 ++-- src/cmd/compile/internal/syntax/tokens.go | 1 + 4 files changed, 10 insertions(+), 4 deletions(-)
  21. ؔ਺ͷܕ΋Ͱ͔͚ΔΑ͏ʹ͠Α͏ var f: ->(int) int = ->(x int) int {

    return x * 2 }
  22. %FNP package main import "fmt" func main() { f :=

    ->(x int, y int) int { return x + y } fmt.Printf("%d\n", f(3, 4)) fmt.Printf("%d", higherFunc(f)) } func higherFunc(f ->(x int, y int) int) int { return f(1, 2) }
  23. Ͱɺ࢖͑ͦ͏ʁ

  24. ΈΜͳେ޷͖IUUQ4FSWFS http.HandleFunc("/", ->(w http.ResponseWriter, r *http.Request) { ... })

  25. ΋͠(Pʹ(FOFSJDT͕ೖͬͨΒ var arr = [1, 2, 3] arr.map( ->(x int)

    int { return x * 2 })
  26. AGVODAΑΓจࣈݮͬͨ

  27. ͋Μ·ΓมΘΒͳ͍ͷͰ͸ʜʜ

  28. ͨͱ͑͹ɺ+BWBͰ͸Ҿ਺ͷܕએݴΛ লུͰ͖Δ ↓ +BWB Function<Integer, Integer> f = (Integer x)

    -> { return x * 2; }; Function<Integer, Integer> f = (x) -> { return x * 2; };
  29. 4DBMBͰ͸ฦΓ஋ܕΛਪ࿦ͯ͘͠ΕΔ ↓ 4DBMB val f: Int => Int = (x:

    Int) => { x * 2 } val f = (x: Int) => { x * 2 }
  30. SFUVSOΛলུͰ͖Δݴޠ΋ଟ͍ ↓ +BWB Function<Integer, Integer> f = (x) -> {

    return x * 2; }; Function<Integer, Integer> f = (x) -> { x * 2; };
  31. ؔ਺͕࢖ΘΕΔ৔ॴͷܕ͕Θ͔͍ͬͯΔ৔߹͸ ؔ਺ࣗମͷҾ਺΍ฦΓ஋ΛলུͰ͖Δ 4DBMB val arr = Array(1, 2) // Array<Int>#mapͷҾ਺͸

    // (Int) => AͳͷͰҾ਺͕Intͱਪ࿦ɺ // ؔ਺ຊମ͔ΒฦΓ஋͕Intͱਪ࿦ arr.map((x) => { x * 2 })
  32. ແ໊Ҿ਺1MBDFIPMEFSͳͲɺ ΋ͬͱলུͰ͖Δ࢓ֻ͚΋ʜ 4DBMB val arr = Array(1, 2) // ແ໊ؔ਺ͷҾ਺Λ

    _ Ͱड͚Δ arr.map(_ * 2)
  33. ແ໊ؔ਺Λ؆ܿ໌ྎʹॻͨ͘Ίʹ͸ ౶ҥߏจͷଞʹ֤छͷলུه๏΋ - Argument Type Inference - Return Type Inference

    - implicit return statement - anonymous argument placeholder
  34. ͦ΋ͦ΋ͦ͜·Ͱলུ͍ͨ͠

  35. Α͏͢Δʹ Conclusion

  36. ؾ࣋ͪΑ͘ແ໊ؔ਺Λॻͨ͘Ίʹ͸ ߟ͑Δ͜ͱ͕ଟ͍ - Syntax - Type Inference - etc

  37. (Pʹ͋ͬͨจ๏͸ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔