|
| 1 | +// These are members of class bar.C, completely unrelated to class foo.A. |
| 2 | +// The types shown below include types defined within foo.A which are: |
| 3 | +// |
| 4 | +// - qualified private |
| 5 | +// - qualified protected |
| 6 | +// - object protected |
| 7 | +// |
| 8 | +// val a : foo.A = { /* compiled code */ } |
| 9 | +// val xprot1 : java.lang.Object with foo.A.FooProt1 = { /* compiled code */ } |
| 10 | +// val xprot2 : java.lang.Object with foo.A.FooProt2 = { /* compiled code */ } |
| 11 | +// val xprot3 : java.lang.Object with foo.A.FooProt3 = { /* compiled code */ } |
| 12 | +// val xprot4 : java.lang.Object with foo.A.FooProt4 = { /* compiled code */ } |
| 13 | +// val xpriv3 : java.lang.Object with foo.A.FooPriv3 = { /* compiled code */ } |
| 14 | +// val xpriv4 : java.lang.Object with foo.A.FooPriv4 = { /* compiled code */ } |
| 15 | +// |
| 16 | +// Indeed it will tell me a type which I cannot access: |
| 17 | +// |
| 18 | +// scala> new bar.C |
| 19 | +// res0: bar.C = bar.C@1339a0dc |
| 20 | +// |
| 21 | +// scala> res0.xpriv3 |
| 22 | +// res1: java.lang.Object with res0.a.FooPriv3 = bar.C$$anon$29@39556aec |
| 23 | +// |
| 24 | +// scala> new res0.a.FooPriv3 |
| 25 | +// <console>:9: error: trait FooPriv3 in class A cannot be accessed in foo.A |
| 26 | +// new res0.a.FooPriv3 |
| 27 | +// ^ |
| 28 | +// Looking at how the compiler prints the types of those vals, one |
| 29 | +// develops a suspicion how some of it is being allowed: |
| 30 | +// |
| 31 | +// val xpriv4: C.this.a.FooPriv4 |
| 32 | +// val xpriv3: C.this.a.FooPriv3 |
| 33 | +// val xprot4: C.this.a.FooProt4 |
| 34 | +// val xprot3: C.this.a.FooProt3 |
| 35 | +// val xprot2: C.this.a.FooProt2 |
| 36 | +// val xprot1: C.this.a.FooProt1 |
| 37 | +// |
| 38 | +// That is, "this" is in the prefix somewhere, it's just not a "this" |
| 39 | +// which has any bearing. |
| 40 | + |
| 41 | +package foo { |
| 42 | + class A { |
| 43 | + trait Foo |
| 44 | + |
| 45 | + protected trait FooProt1 |
| 46 | + protected[this] trait FooProt2 |
| 47 | + protected[foo] trait FooProt3 |
| 48 | + protected[A] trait FooProt4 |
| 49 | + |
| 50 | + private trait FooPriv1 |
| 51 | + private[this] trait FooPriv2 |
| 52 | + private[foo] trait FooPriv3 |
| 53 | + private[A] trait FooPriv4 |
| 54 | + |
| 55 | + type BarProt1 = FooProt1 |
| 56 | + type BarProt2 = FooProt2 |
| 57 | + type BarProt3 = FooProt3 |
| 58 | + type BarProt4 = FooProt4 |
| 59 | + |
| 60 | + // type BarPriv1 = FooPriv1 |
| 61 | + // type BarPriv2 = FooPriv2 |
| 62 | + type BarPriv3 = FooPriv3 |
| 63 | + type BarPriv4 = FooPriv4 |
| 64 | + |
| 65 | + def fprot1(x: FooProt1) = x |
| 66 | + def fprot2(x: FooProt2) = x |
| 67 | + def fprot3(x: FooProt3) = x |
| 68 | + def fprot4(x: FooProt4) = x |
| 69 | + |
| 70 | + // def fpriv1(x: FooPriv1) = x |
| 71 | + // def fpriv2(x: FooPriv2) = x |
| 72 | + def fpriv3(x: FooPriv3) = x |
| 73 | + def fpriv4(x: FooPriv4) = x |
| 74 | + |
| 75 | + val yprot1 = new FooProt1 { } |
| 76 | + val yprot2 = new FooProt2 { } |
| 77 | + val yprot3 = new FooProt3 { } |
| 78 | + val yprot4 = new FooProt4 { } |
| 79 | + |
| 80 | + // val ypriv1 = new FooPriv1 { } |
| 81 | + // val ypriv2 = new FooPriv2 { } |
| 82 | + val ypriv3 = new FooPriv3 { } |
| 83 | + val ypriv4 = new FooPriv4 { } |
| 84 | + |
| 85 | + def fpriv_alt1(x: FooPriv1) = 0 // !!! isn't the private type now in the signature of the (public) method? |
| 86 | + def fpriv_alt2(x: FooPriv2) = 0 // !!! isn't the private[this] type now in the signature of the (public) method? |
| 87 | + } |
| 88 | + // Same package, subclass |
| 89 | + class B extends A { |
| 90 | + val xprot1 = new BarProt1 { } |
| 91 | + val xprot2 = new BarProt2 { } |
| 92 | + val xprot3 = new BarProt3 { } |
| 93 | + val xprot4 = new BarProt4 { } |
| 94 | + |
| 95 | + // val xpriv1 = new BarPriv1 { } |
| 96 | + // val xpriv2 = new BarPriv2 { } |
| 97 | + val xpriv3 = new BarPriv3 { } |
| 98 | + val xpriv4 = new BarPriv4 { } |
| 99 | + |
| 100 | + override def fprot1(x: BarProt1) = x |
| 101 | + override def fprot2(x: BarProt2) = x |
| 102 | + override def fprot3(x: BarProt3) = x |
| 103 | + override def fprot4(x: BarProt4) = x |
| 104 | + |
| 105 | + // override def fpriv1(x: BarPriv1) = x |
| 106 | + // override def fpriv2(x: BarPriv2) = x |
| 107 | + override def fpriv3(x: BarPriv3) = x |
| 108 | + override def fpriv4(x: BarPriv4) = x |
| 109 | + } |
| 110 | + // Same package, unrelated class |
| 111 | + class C { |
| 112 | + val a = new A |
| 113 | + import a._ |
| 114 | + |
| 115 | + val xprot1 = new BarProt1 { } |
| 116 | + val xprot2 = new BarProt2 { } |
| 117 | + val xprot3 = new BarProt3 { } |
| 118 | + val xprot4 = new BarProt4 { } |
| 119 | + |
| 120 | + // val xpriv1 = new BarPriv1 { } |
| 121 | + // val xpriv2 = new BarPriv2 { } |
| 122 | + val xpriv3 = new BarPriv3 { } |
| 123 | + val xpriv4 = new BarPriv4 { } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +package bar { |
| 128 | + // Different package, subclass |
| 129 | + class B extends foo.A { |
| 130 | + val xprot1 = new BarProt1 { } |
| 131 | + val xprot2 = new BarProt2 { } |
| 132 | + val xprot3 = new BarProt3 { } |
| 133 | + val xprot4 = new BarProt4 { } |
| 134 | + |
| 135 | + // val xpriv1 = new BarPriv1 { } |
| 136 | + // val xpriv2 = new BarPriv2 { } |
| 137 | + val xpriv3 = new BarPriv3 { } |
| 138 | + val xpriv4 = new BarPriv4 { } |
| 139 | + |
| 140 | + override def fprot1(x: BarProt1) = x |
| 141 | + override def fprot2(x: BarProt2) = x |
| 142 | + override def fprot3(x: BarProt3) = x |
| 143 | + override def fprot4(x: BarProt4) = x |
| 144 | + |
| 145 | + // override def fpriv1(x: BarPriv1) = x |
| 146 | + // override def fpriv2(x: BarPriv2) = x |
| 147 | + override def fpriv3(x: BarPriv3) = x |
| 148 | + override def fpriv4(x: BarPriv4) = x |
| 149 | + } |
| 150 | + // Different package, unrelated class |
| 151 | + class C { |
| 152 | + val a = new foo.A |
| 153 | + import a._ |
| 154 | + |
| 155 | + val xprot1 = new BarProt1 { } |
| 156 | + val xprot2 = new BarProt2 { } |
| 157 | + val xprot3 = new BarProt3 { } |
| 158 | + val xprot4 = new BarProt4 { } |
| 159 | + |
| 160 | + // val xpriv1 = new BarPriv1 { } |
| 161 | + // val xpriv2 = new BarPriv2 { } |
| 162 | + val xpriv3 = new BarPriv3 { } |
| 163 | + val xpriv4 = new BarPriv4 { } |
| 164 | + } |
| 165 | +} |
0 commit comments