32

Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Embed Size (px)

Citation preview

Page 1: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016
Page 2: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016
Page 3: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

we’re hiring!troops.ai/careers

Page 4: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Goal

Understand the Scala compiler by tracing the

compilation of a simple program and concentrating on a

single feature

Page 5: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Agenda

1. What is Implicit Conversion?

2. Result & RichResult

3. Compiler Data Structures & Phases

4. parser Phase

5. namer Phase

6. typer Phase

Page 6: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Implicit Conversion

1. Scala Language Specification 7.3

2. Scala Improvement Process 13

“In a selection e.m with e of type T, if the selector m does not denote an accessible member of T … a view v is searched which is applicable to e and whose result contains a member named m.”

“An implicit class is desugared into a class and implicit method pairing, where the implicit method mimics the constructor of the class.”

Page 7: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Implicit Conversion

1. Scala Language Specification 7.3

2. Scala Improvement Process 13

“In a selection e.m with e of type T, if the selector m does not denote an accessible member of T … a view v is searched which is applicable to e and whose result contains a member named m.”

“An implicit class is desugared into a class and implicit method pairing, where the implicit method mimics the constructor of the class.”

Page 8: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Result & RichResult

Page 9: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Result & RichResult

Page 10: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 11: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 12: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 13: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 14: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 15: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Data Structures

1. Compilation Unit

2. Context

3. Tree

4. Symbol

5. Type

6. Scope

Page 16: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Compiler Phases

1. parser

2. namer

3. typer

Page 17: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Compiler Phases

1. parser

2. namer

3. typer

Page 18: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Compiler Phases

1. parser

2. namer

3. typer

Page 19: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

parser Phase

Page 20: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

parser Phase

...

Page 21: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

namer Phase

Page 22: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

namer Phase - enterImplicitWrapper

Page 23: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase

Page 24: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase

Page 25: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - Select

Page 26: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - Select

Page 27: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - View

Page 28: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - View

Page 29: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - View

Page 30: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

typer Phase - View

Page 31: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

The End

Page 32: Troops.ai: Implicit Class - The Machinery Behind the Semantics - ny-scala October 2016

Thanks