Objects and Modules – Two sides of the same coin?
Martin Odersky
Typesafe and EPFL Milner Symposium, 16 April 2012
1
Objects and Modules Two sides of the same coin? Martin Odersky - - PowerPoint PPT Presentation
Objects and Modules Two sides of the same coin? Martin Odersky Typesafe and EPFL Milner Symposium, 16 April 2012 1 Components Modules/Objects Compilers Reflection 2 Modules vs Objects Modules and Objects have the same purpose:
1
2
3
4
5
6
7
8
9
trait ¡AbsCell ¡{ ¡ ¡ type ¡T ¡ val ¡init: ¡T ¡ ¡ private ¡var ¡value ¡: ¡T ¡= ¡init ¡ ¡ def ¡get: ¡T ¡= ¡value ¡ ¡ def ¡set(x: ¡T) ¡= ¡{ ¡value ¡= ¡x ¡} ¡ } ¡ ¡
val ¡cell ¡= ¡new ¡AbsCell ¡{ ¡type ¡T ¡= ¡Int; ¡val ¡init ¡= ¡1 ¡} ¡ cell.set(cell.get ¡* ¡2) ¡
10
def ¡reset(c ¡: ¡AbsCell): ¡unit ¡= ¡c.set(c.init); ¡ ¡
– c.init has type c.T.
11
12
¡ ¡ ¡object ¡Types ¡{ ¡ ¡ ¡trait ¡Symbol ¡{ ¡ ¡ ¡ ¡ ¡trait ¡Type ¡{ ¡ ¡ ¡ ¡ ¡def ¡tpe ¡: ¡Types.Type ¡ ¡ ¡ ¡ ¡ ¡def ¡sym ¡: ¡Symbols.Symbol ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡... // static data for symbols ¡ ¡ ¡ ¡ ¡... // static data for types } ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
13
trait ¡SymbolTable ¡{ ¡ ¡ ¡object ¡Symbols ¡{ ¡ ¡ ¡trait ¡Symbol ¡{ ¡def ¡tpe ¡: ¡Types.Type; ¡... ¡} ¡ ¡ ¡} ¡ ¡ ¡object ¡Types ¡{ ¡ ¡ ¡trait ¡Type ¡{ ¡def ¡sym ¡: ¡Symbols.Symbol; ¡... ¡} ¡ ¡ ¡} ¡ } ¡
14
15
trait ¡Symbols ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡trait ¡Types ¡{ ¡ ¡ ¡ ¡type ¡Type ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡type ¡Symbol ¡ ¡ ¡trait ¡Symbol ¡{ ¡def ¡tpe: ¡Type ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡trait ¡Type ¡{ ¡def ¡sym: ¡Symbol ¡} ¡ } ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡
¡ ¡ trait ¡SymbolTable ¡extends ¡Symbols ¡with ¡Types ¡ ¡
16
trait ¡Symbols ¡{ ¡this: ¡Types ¡with ¡Symbols ¡=> ¡ ¡ ¡trait ¡Symbol ¡{ ¡def ¡tpe: ¡Type ¡} ¡ } ¡ trait ¡Types ¡{ ¡this: ¡Symbols ¡with ¡Types ¡=> ¡ ¡ ¡trait ¡Type ¡{ ¡def ¡symbol ¡} ¡ } ¡
17
trait ¡C ¡{ ¡this: ¡T ¡=> ¡... ¡} ¡ ¡ ¡T is called a self-type of trait C.
18
20
21
22
¡ ¡ ¡reflect.api.Universe ¡# ¡Symbol ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡reflect.mirror.Symbol ¡ ¡ ¡≠ ¡ ¡ ¡remote.mirror.Symbol ¡ ¡
23
24
25
26
27
reflect.internal.Universe ¡ nsc.Global ¡(scalac) reflect.runtime.Mirror ¡
28
reflect.internal.Universe ¡ nsc.Global ¡(scalac) reflect.runtime.Mirror ¡ reflect.api.Universe ¡/ ¡ reflect.mirror ¡
29
30
31
32
33
34
35
36
37
38
39
40
41
<[ ¡if ¡(!cond) ¡raise(msg) ¡]> ¡
42
43
44
45
Types prevent “silly mistakes” that come from confusing staging times raise is now type-checked at macro-expansion type, hence hygienic.
46