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 :
- Make
c
aval
instead of avar
- declare
is_door_open
parameter as d: Car#Door - Stop making
Door
dependant on instance ofCar
, declare it outside the class, if you want it to beCar.Door
, put it in a companionobject Car
rather than inclass Car
- Make
is_door_open
a method ofDoor
(without aDoor
argument then) rather than Car. It has full access to the enclosing Car (withCar.this
as in java, or declaring an alias for this in Car withclass Car {car =>
)
精彩评论