4

There's a simple example in Programming in Scala by Odersky et al on abstract types, but it does not seem to follow to it's logical conclusion [now edited to make this my exact code]:

class Food
class Grass extends Food
class FishFood extends Food

abstract class Animal {
  type Feed <: Food
  def eat(food: Feed)
}

class Cow extends Animal {
  type Feed = Grass
  override def eat(food: Grass) = {}
}

class Test extends App {
  val cow: Animal = new Cow
  cow.eat(new FishFood)
  cow.eat(new Grass)
}

They explain this will prevent me doing (as above):

val cow: Animal = new Cow
cow.eat(new FishFood)

So far so good. But the next natural step does not seem to work either:

cow.eat(new Grass)

I get a compile error:

type mistmatch;
found : Grass
required: Test.this.cow.Feed
 cow.eat(new Grass)
         ^

But cow.Feed is Grass, so why doesn't this work?

4
  • 2
    I cannot reproduce it. I can feed my cow :) Please provide the erroneous code as well. Aug 27, 2012 at 12:32
  • What version of Scala? I just saw in the REPL for Scala 2.9.0.1 that this does indeed happen. It might be a bug in that version...
    – Don Roby
    Aug 27, 2012 at 12:41
  • @lulian The listing now shows what I'm trying to compile Aug 27, 2012 at 12:49
  • @Don now tried in 2.10 M6 and 2.9.2 on Windows Aug 27, 2012 at 12:49

2 Answers 2

8

The problem here is that your val cow is typed as Animal rather than Cow, so all that the compiler knows is that its eat method expects some specific subtype of Food, but it doesn't know which, and in particular it's unable to prove that that type is equal to Grass.

You can see the difference this makes to the type of the method (as viewed from Animal vs. as viewed from Cow) by asking for it's eta-expansion,

scala> val cow: Animal = new Cow
cow: Animal = Cow@13c02dc4

scala> cow.eat _
res12: cow.Feed => Unit = <function1>

scala> cow.asInstanceOf[Cow].eat _
res13: Grass => Unit = <function1>

You'll notice that in the second, more precisely typed, case the compiler views the method as taking an argument of type Grass rather than the abstract type cow.Feed.

2
  • This is interesting as it seems to highlight a difference between type parameters and abstract types. If I had the Food as a parameter, then I could remain abstract about the animal, but not the feed by having Animal[Grass] as my type, and still call eat(new Grass) on this. Is there no way to do something similar with abstract types in Scala? Aug 27, 2012 at 13:12
  • Yes, you could declare your cow with a refinement: val cow : Animal { type Feed = Grass } = new Cow. Aug 27, 2012 at 14:18
3

And it should fail in such way - and actually, I think it is explained in Programming in Scala (2nd edition, p. 460).

On this line:

val cow: Animal = new Cow

You told the compiler to assume that cow could be any animal, including fish - and it certainly would do fish no good to eat grass!

If you would let the compiler to infer the proper type, it would compile:

val cow = new Cow
cow.eat(new Grass)
1
  • Thanks for the response. I realise that having a Cow, rather than an Animal type will allow me to feed my cow. But I thought the path-dependent type of Cow.Feed of my cow instance will allow the instance to take the correct parameter type. In the book they only use the example of trying to feed their cow FishFood Aug 27, 2012 at 13:02

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.