Scala n00b here. Pretty sure I understand PDT's but was making sure and hit a problem; here's a previous question Scala types: Class A is not equal to the T where T is: type T = A with sample code which I'll reproduce here:
1: class Food
2: class Fish extends Food
3: class Grass extends Food
4:
5: abstract class Animal {
6: type SuitableFood <: Food
7: def eat(food: SuitableFood)
8: }
9:
10: class Cow extends Animal {
11: type SuitableFood = Grass
12: override def eat(food: Grass) {}
13: }
14:
15: val bessy: Animal = new Cow // [1]
16:
17: bessy eat (new bessy.SuitableFood) // [2]
The original poster said this compiled, I believe it should, but it won't. If I paste it into the scala REPL it successfully creates bessy [1]:
scala> val bessy: Animal = new Cow
bessy: Animal = Cow@165b8a71
but [2], gives me an error which I don't understand:
scala> bessy.eat(bessy.SuitableFood)
<console>:17: error: value SuitableFood is not a member of Animal
bessy.eat(bessy.SuitableFood)
^
If I paste it into a file and 'scalac' it, I get the same. Why? bessy
is a cow object, type SuitableFood = Grass
is defined therein, bessy.SuitableFood
is a a class type (isn't it?).
What's wrong?