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 4590400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:14:47+00:00 2026-05-21T22:14:47+00:00

The following doesn’t compile. Do I need to cast the person first? object People

  • 0

The following doesn’t compile. Do I need to cast the person first?

 object People {
  def all = List(
    new Person("Jack", 33),
    new Person("John", 31) with Authority,
    new Person("Jill", 21),
    new Person("Mark", 43)
  )
}

class Person(val name: String, val age: Int) 

trait Authority {
  def giveOrder {
    println("do your work!")
  }
}

object Runner {
  def main(args:List[String]) {
    val boss = People.all.find { _.isInstanceOf [Authority] }.get
    boss.giveOrder // This line doesnt compile
  }
}
  • 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-05-21T22:14:48+00:00Added an answer on May 21, 2026 at 10:14 pm

    You’re right thinking that somehow, there should be a mechanism that lets you avoid casting. Such a cast would be ugly and redundant, as it already appears in the filter anyway. find, however, does not care at all about the shape of the predicate it gets; it just applies it and returns an Option[A] if A is the static type of the collection’s elements.

    What you need is the collect function:

    val boss = People.all.collect { case boss: Authority => boss }.head
    

    collect creates a new collection. If you want to avoid this (if you’re really only interested in the first element which is of kind Authority) in case of a potentially very long list of potential bosses, you may want to switch to a view to have it evaluated lazily:

    val boss = People.all.view.collect { case boss: Authority => boss }.head
    

    Finally, unless you’re absolutely sure that there is always at least one boss in your list, you should really test whether or not the search was successful, e.g. like this:

    val bossOpt = People.all.view.collect { case boss: Authority => boss }.headOption
    bossOpt.foreach(_.giveOrder) // happens only if a boss was found
    

    Edit: Finally, if you’re using Scala 2.9, you should definitely use collectFirst as explained in Kevin Wright’s answer.

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

Sidebar

Related Questions

The following code doesn't work (of course), because the marked line does not compile:
The following doesn't compile, how can I do this? I think the example shows
Why doesn't the following doesn't handle the exception that was rethrown? I tried all
I'm trying to use STL, but the following doesn't compile. main.cpp : #include <set>
The following doesn't compile: public void MyMethod<T>(T value) { if (value == default(T)) {
Any idea why the following doesn't work? (R3) o: make object! [ foo: does
Can someone explain why the following doesn't compile? I want that BB[A] is also
I need to know what version of reportlab I'm running. The following doesn't seem
Following doesn't work. I need to add a , and an input param at
The following doesn't compile, but how can I get the equivalent functionality of [MYDIR]

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.