Skip to content

Instantly share code, notes, and snippets.

@fff
Created November 16, 2010 12:52
Show Gist options
  • Save fff/701793 to your computer and use it in GitHub Desktop.
Save fff/701793 to your computer and use it in GitHub Desktop.
why generic failed?
class Banana
object Monkey {
def eat(bananas: Seq[Banana]) = println("lots of banana")
def eat(banana: Banana) = println("one banana")
}
object Person {
def pickBanana = new Banana
def pickBananas = List(new Banana, new Banana)
}
object FeedMonkey {
def feed[T](food: => T, eat: T => Unit): Unit = eat(food)
def main(args: Array[String]) {
//below lines are failed
/* error:ambiguous reference to overloaded definition, both method eat in object Monkey of
type (banana: Banana)Unit and method eat in object Monkey of type (bananas: Seq[Banana])Unit match expected
type (?) => Unit */
feed(Person.pickBanana, Monkey.eat)
feed(Person.pickBananas, Monkey.eat)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment