Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

class Car {  
    var door = new Door(true)  

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

    def is_door_open(d: Door) {  
         println(d.state)  
    }  
}
like image 915
c141charlie Avatar asked Jan 18 '26 14:01

c141charlie


1 Answers

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 => )
like image 68
Didier Dupont Avatar answered Jan 21 '26 05:01

Didier Dupont



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!