3

When I was trying the Animal/Food example for abstract types in Martin Odersky's Programming in Scala,

class Food
abstract class Animal {
  type SuitableFood <: Food
  def eat(food:SuitableFood)
}
class Grass extends Food
class Cow extends Animal {
  type SuitableFood=Grass
  override def eat(food:SuitableFood) {}
}
val bessy:Animal = new Cow
bessy.eat(new Grass)

I got the following error:

scala> <console>:13: error: type mismatch;
 found   : Grass
 required: bessy.SuitableFood
                  bessy.eat(new Grass)
                            ^

The original example by Martin was bessy.eat(new Fish), which would definitely fail, but I didn't expect it'd fail for Grass as well. The above error can be avoided by letting bessy be Cow instead of Animal: val bessy:Cow = new Cow.

Does this mean dynamic binding doesn't work here?

Edited: Simple dynamic binding for regular inheritance in Scala:

abstract class Parent {
  def sig:String = "Parent"
}
class Child extends Parent {
  override def sig:String = "Child"
}

And I had this, where x:Parent gave Child as well:

scala> new Child().sig
res1: String = Child

val x:Parent = new Child()
x: Parent = Child@3a460b07

x.sig
res2: String = Child
lcn
  • 2,239
  • 25
  • 41

1 Answers1

7

Scala is statically typed. An arbitrary animal cannot eat grass, and you have just tried to feed grass to an arbitrary animal. It happens to be a cow, but you have stated (with : Animal) that the compiler may only assume that it is an animal.

If you allow the compiler to know that bessy is a Cow (val bessy = new Cow), then she'll eat grass just fine.

Rex Kerr
  • 166,841
  • 26
  • 322
  • 407
  • Well, am I taking the dynamic binding concept wrong? `List` in Java has `LinkedList` and `ArrayList`, so when you call a `xs.get(idx)` on a `List`, you get the `ArrayList` version when it's an `ArrayList` implementation. I didn't expect Scala would deviate from this. – lcn Dec 24 '13 at 03:03
  • @lcn - Types must match. Your "simple" example has the same type signature for `Parent.sig` and `Child.sig`. Inheritance (of the "dynamic binding" type in C++) works just fine, but you must obey the types you've given. – Rex Kerr Dec 24 '13 at 10:49
  • so you mean when abstract type involved, the `eat` methods will have *different* signatures and thus are different methods? I understand this suits better for the purpose of abstract types, but it should really be documented that there is *NO* dynamic binding here. – lcn Dec 24 '13 at 19:23
  • @lcn - It is dynamic binding (as the term is used in C++; this means different things in other contexts) at runtime, as the appropriate method is selected from the method table. However, at compile-time, the compiler will stop you from feeding grass to an arbitrary animal. – Rex Kerr Dec 24 '13 at 19:34