3

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)  
    }  
}
3
  • Which Scala version? Works (after removing = true default value) on 2.7.7final... Commented Sep 21, 2011 at 20:55
  • Scala version is 2.9.0.1 Commented Sep 21, 2011 at 21:01
  • @Tomasz : I suspect that out of (good) habit, you declared a val c rather than var c Commented Sep 21, 2011 at 21:29

1 Answer 1

6

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 => )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the help. Now I get what Scala means when it uses the # symbol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.