yuroyoro
October 28, 2019
2.8k

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

GoCon 2019 LT

October 28, 2019

## Transcript

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

Go !ZVSPZPSP (Pʹແ໊ؔ਺ͷ౶ҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ

* 2 }

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 }

}

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

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

SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE

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?

2 }
19. ### AGVODAΛAAʹม͑Δ͚ͩ ->(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) }

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

int { return x * 2 })

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