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 a val instead of a var
- declare
is_door_open parameter as d: Car#Door
- 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
- 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 => )
= truedefault value) on 2.7.7final...