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

  • Home
  • SEARCH
  • 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 8202019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:01:09+00:00 2026-06-07T07:01:09+00:00

I have two variables of unknown type. I want to do a pattern match

  • 0

I have two variables of unknown type. I want to do a pattern match on the combination of the variable types, and then take action based on that — specifically, I want to implement Comparator[Any] and compare the two variables based on their type. This code produces the desired result:

class SomethingComparator extends util.Comparator[Any] {
  override def compare(o1: Any, o2: Any) = List(o1, o2).map(_.isInstanceOf[Something]) match {
    case List(true, true) => o1.asInstanceOf[Something].someInt.compareTo(o2.asInstanceOf[Something].someInt)
    case List(true, false) => -1
    case List(false, true) => 1
    case _ => 0
  }
}

Is there a way to avoid the map call and pattern match on the types directly, and therefore avoid the two asInstanceOf calls in the first match?

  • 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-07T07:01:11+00:00Added an answer on June 7, 2026 at 7:01 am

    This works:

    class SomethingComparator extends util.Comparator[Any] {
      override def compare(o1: Any, o2: Any) = (o1, o2) match {
        case (o1:Something, o2:Something) => o1.someInt.compareTo(o2.someInt)
        case (o1:Something, o2:Any) => -1
        case (o1:Any, o2:Something) => 1
        case _ => 0
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two variables, key and value , and I want to add them
I have two variables: char charTime[] = TIME; char buf[] = SOMETHINGELSE; I want
I have two environment variables in apache config, I want to concatenate them into
I have two variables, 'a' and 'b' in my JavaScript, and i want to
I have two variables of type int? (or Nullable<int> if you will). I wanted
I have two variables of type ILookup. I wanted to use Union or Concat
I have two variables like : <script language=javascript type=text/javascript> var count = 1; calc_my_project_
I have two integer variables i and j and I want to make a
I have two variables in a jquery code and if item.a not exist, it
I have two variables: unsigned short a,b; /* When I compare them with a

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.