File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change 1+ abstract class Source [A ] { self =>
2+ def consume (a : A ): Int
3+ def contramap [B ](f : B => A ): Source [B ] = {
4+ new Source [B ] { // OfClass($anon).outerValue = {OfClass(Source), OfClass($anon)} ???
5+ override def consume (b : B ) = self.consume(f(b))
6+ }
7+ }
8+ }
9+
10+ object O {
11+ val identity : Source [Int ] = new Source [Int ] {
12+ override def consume (a : Int ): Int = a
13+ } // OfClass(Source[A])
14+ val longToInt : Source [Long ] = identity.contramap((l : Long ) => l.toInt) // longToInt.outer == identity
15+ val doubleToLongToInt : Source [Double ] = longToInt.contramap((d : Double ) => (d + 2.4 ).toLong) // doubleToLongToInt == longToInt
16+ // OfClass(Source[Double]).outer = {LocalEnv(contramap)};
17+ // LocalEnv(contramap).outer = {OfClass(Source[Long]), OfClass(Source[Double])}
18+ println(doubleToLongToInt.consume(3.5 ))
19+ }
20+
Original file line number Diff line number Diff line change 1+ class A (val x : Int ) {
2+ class B {
3+ println(" A.this = " + A .this .hashCode()) // `a`
4+ println(" A.this.x = " + A .this .x) // B --> outer A (42 or 46)
5+ def fooz = x
6+ def fooz2 = x
7+ class D {
8+ println(" B.this = " + B .this .hashCode()) // `c` in `foo`
9+ def bar = fooz // expands to B.this.fooz, calls fooz in class B
10+ def bar2 = fooz2 // expands to B.this.fooz, calls fooz2 in class C
11+ }
12+ }
13+ }
14+ class AA (x : Int ) extends A (x) {
15+ def foo = {
16+ val a = if true then new A (42 ) else new AA (46 )
17+ println(" a = " + a.hashCode())
18+ class C /* outer: AA(44) (`Main.aa`)*/ extends a.B /* outer: A(42) or AA(46) (`a`)*/ {
19+ println(" AA.this = " + AA .this .hashCode()) // Main.aa
20+ println(" AA.this.x = " + x) // C --> outer AA --> parent A (44)
21+ override def fooz2 = x // 44
22+ }
23+ val c : C = new C
24+ println(" c = " + c.hashCode())
25+ val d = new c.D // outer: C (`c`)
26+ println(" d.bar = " + d.bar + " , d.bar2 = " + d.bar2)
27+ d.bar + d.bar2
28+ }
29+ }
30+
31+ object O {
32+ val aa = new AA (44 )
33+ val f = aa.foo
34+ println(" aa = " + aa.hashCode())
35+ println(" f = " + f)
36+ }
37+
38+ object Main {
39+ def main (args : Array [String ]) = {
40+ O
41+ ()
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments