Slide 1

Slide 1 text

Considering the introduction of the anonymous function syntax sugar into Go !ZVSPZPSP (Pʹແ໊ؔ਺ͷ౶ҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ

Slide 2

Slide 2 text

(Pͷແ໊ؔ਺ var f = func(x int) int { return x * 2 }

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

੩తܕ෇͖ͳݴޠͰ͸ʁ const f = (x:number): number => { return x * 2 } 5ZQF4DSJQU +BWB 4DBMB Function f = (Integer x) -> { return x * 2; }; val f: (Int) => Int = (x) => { x * 2 }

Slide 6

Slide 6 text

(PͰ͸͜ΜͳܗͷߏจʹͳΓͦ͏ (name type, …) type => { function body }

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

ίϯύΠϥʹ͸ͭͷϑΣʔζ͕͋Δ 1. Parsing 2. Type-checking and AST transformations 3. Generic SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

(Pͷߏจղੳ LL(1)

Slide 12

Slide 12 text

͍͍ͤͥͻͱͭઌͷτʔΫϯ͔͠ઌಡΈ͠ͳ͍ var f = ( x * 2 ) ↑ current: ( : _Lparen next : x : _Name

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

"SSPX'VODUJPO͕ೖΔͱʜʜ var f = ( x int ) int => { … } ↑ current: ( : _Lparen next : x : _Name

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Ͳ͏ʹ͔ͳΒͳ͍

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

ॻ͍ͯΈͨ 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(-)

Slide 21

Slide 21 text

ؔ਺ͷܕ΋Ͱ͔͚ΔΑ͏ʹ͠Α͏ var f: ->(int) int = ->(x int) int { return x * 2 }

Slide 22

Slide 22 text

%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) }

Slide 23

Slide 23 text

Ͱɺ࢖͑ͦ͏ʁ

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

AGVODAΑΓจࣈݮͬͨ

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

ͨͱ͑͹ɺ+BWBͰ͸Ҿ਺ͷܕએݴΛ লུͰ͖Δ ↓ +BWB Function f = (Integer x) -> { return x * 2; }; Function f = (x) -> { return x * 2; };

Slide 29

Slide 29 text

4DBMBͰ͸ฦΓ஋ܕΛਪ࿦ͯ͘͠ΕΔ ↓ 4DBMB val f: Int => Int = (x: Int) => { x * 2 } val f = (x: Int) => { x * 2 }

Slide 30

Slide 30 text

SFUVSOΛলུͰ͖Δݴޠ΋ଟ͍ ↓ +BWB Function f = (x) -> { return x * 2; }; Function f = (x) -> { x * 2; };

Slide 31

Slide 31 text

ؔ਺͕࢖ΘΕΔ৔ॴͷܕ͕Θ͔͍ͬͯΔ৔߹͸ ؔ਺ࣗମͷҾ਺΍ฦΓ஋ΛলུͰ͖Δ 4DBMB val arr = Array(1, 2) // Array#mapͷҾ਺͸ // (Int) => AͳͷͰҾ਺͕Intͱਪ࿦ɺ // ؔ਺ຊମ͔ΒฦΓ஋͕Intͱਪ࿦ arr.map((x) => { x * 2 })

Slide 32

Slide 32 text

ແ໊Ҿ਺1MBDFIPMEFSͳͲɺ ΋ͬͱলུͰ͖Δ࢓ֻ͚΋ʜ 4DBMB val arr = Array(1, 2) // ແ໊ؔ਺ͷҾ਺Λ _ Ͱड͚Δ arr.map(_ * 2)

Slide 33

Slide 33 text

ແ໊ؔ਺Λ؆ܿ໌ྎʹॻͨ͘Ίʹ͸ ౶ҥߏจͷଞʹ֤छͷলུه๏΋ - Argument Type Inference - Return Type Inference - implicit return statement - anonymous argument placeholder

Slide 34

Slide 34 text

ͦ΋ͦ΋ͦ͜·Ͱলུ͍ͨ͠

Slide 35

Slide 35 text

Α͏͢Δʹ Conclusion

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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