- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why does this compile?
Fri, 2011-12-09, 01:45
trying to cause an error, I'm surprised the following code *does* compiles:
http://paste.pocoo.org/show/518169/
The error I was trying to cause rises if you change line 23 from
var iG = i.getArg
to
var iG = i.arg
which shouldn't make a difference because
trait I { selfType : A#B ⇒
val arg : A#C
def getArg = arg
complete code ======= >>>
object mainO {
def main(args: Array[String]) {
val a1 = new A("1")
new O(a1)
}
}
class A(val s: String) {
abstract class B {def c: C = new C}
class C { override def toString = "A: "+A.this.s}
}
trait I { selfType : A#B ⇒
val arg : A#C
def getArg = arg
def iS = c==arg
}
class O(val a: A) {
object i extends a.B with I{val arg = new a2.C}
val a2 = new A("2")
var iG = i.getArg // >> change to i.arg and:
/*
[error] Z:\Coden\sct\sctpi\src\main\scala\spielwiese\A.scala:29: type
mismatch;
[error] found : O.this.a.C
[error] required: O.this.a2.C
[error] iG = iB
*/
var iB = i.c
println("iG "+iG) //2
println("iB "+iB) //1
iG = iB
println("iG' "+iG)//1
}
ok, I got it. One returns the general type A#C, the other one O.this.a.C.
On 09.12.11 01:45, Stefan Kuhn wrote:
> trying to cause an error, I'm surprised the following code *does* compiles:
> http://paste.pocoo.org/show/518169/
>
> The error I was trying to cause rises if you change line 23 from
> var iG = i.getArg
> to
> var iG = i.arg
> which shouldn't make a difference because
> trait I { selfType : A#B ⇒
> val arg : A#C
> def getArg = arg
>
>
> complete code ======= >>>
> object mainO {
> def main(args: Array[String]) {
> val a1 = new A("1")
> new O(a1)
> }
> }
>
> class A(val s: String) {
> abstract class B {def c: C = new C}
> class C { override def toString = "A: "+A.this.s}
> }
>
> trait I { selfType : A#B ⇒
> val arg : A#C
> def getArg = arg
>
> def iS = c==arg
> }
>
> class O(val a: A) {
> object i extends a.B with I{val arg = new a2.C}
> val a2 = new A("2")
> var iG = i.getArg // >> change to i.arg and:
> /*
> [error] Z:\Coden\sct\sctpi\src\main\scala\spielwiese\A.scala:29: type
> mismatch;
> [error] found : O.this.a.C
> [error] required: O.this.a2.C
> [error] iG = iB
> */
> var iB = i.c
> println("iG "+iG) //2
> println("iB "+iB) //1
> iG = iB
> println("iG' "+iG)//1
> }
>
>