Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8505243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:15:48+00:00 2026-06-11T02:15:48+00:00

There’s a simple example in Programming in Scala by Odersky et al on abstract

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T02:15:50+00:00Added an answer on June 11, 2026 at 2:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There must be a simple solution to this, but after 4 hours of browsing
There's a lot of reading on self referencing problems, but I can't seem to
There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There are nice SO question and answers about this issue, but these options didn't
There have been multiple questions regarding this topic but I have never really settled
There are some stdlib functions that throw errors on invalid input. For example: Prelude>
There are many tutorials that talk about deleting index.php from the url. But I
There doesn't seem to be any tried and true set of best practices to
There seem to be many ways to define singletons in Python. Is there a
There are a lot of questions about upgrading here, but I have another, very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.