开发者

Scala: Passing instance of child class to parent class function

Why does the Scala repl say:

<console>:10: error: type mismatch;  
 found   : Car#Door  
 required: _1.Door where val _1: Car  

When I run this statement:

var c = New Car  
c.is_door_open(c.door)  

That refers to this class: 开发者_C百科

class Car {  
    var door = new Door(true)  

    class Door (var state: Boolean = true) {  }    

    def is_door_open(d: Door) {  
         println(d.state)  
    }  
}


When you have an inner class as Door here, each instance of class Car defines a different Door type. In is_door_open(d: Door), Door means a Door of the enclosing instance of Car. The type Car#Door in the error message means a Door of any Car, it is the common type to all doors.

When you call c.is_door_open(c.door) the Car of c.door must be the same as the the c of c.is_door_open, as that is what the declaration of is_door_open requires (it should have been d: Car#Door otherwise). And moreover, they must be the same to the satisfaction of the compiler, which has some precise rules for that. Here it seems obvious the cars are the same. Not so, because c is a var, so not a stable identifier.

Imagine the code c.is_door_open({c = new Car; c.door}). Contrived of course, but this shows that you cannot rely on both occurence of c being the same car.

So among your solutions, depending of what your real code may be :

  1. Make c a val instead of a var
  2. declare is_door_open parameter as d: Car#Door
  3. Stop making Door dependant on instance of Car, declare it outside the class, if you want it to be Car.Door, put it in a companion object Car rather than in class Car
  4. Make is_door_open a method of Door (without a Door argument then) rather than Car. It has full access to the enclosing Car (with Car.this as in java, or declaring an alias for this in Car with class Car {car => )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜