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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:18:25+00:00 2026-06-18T00:18:25+00:00

I was going through some exercises and noticed the following behavior for matching of

  • 0

I was going through some exercises and noticed the following behavior for matching of tuple2. Is there a particular reason for this?

 def test(x: Any): Unit= x match{
  case i: Int => println("int")
  case b: Boolean => println("bool")
  case ti: (_, Int) => println("tuple2 with int")
  case tb: (_, Boolean)=> println("tuple2 with boolean")
  case _ => println("other")
  }                                                

test(false) //prints bool
test(3) ///prints int
test((1,3)) //prints tuple with int
test((1,true)) //prints tuple with int 

If i exchange the ti and tb cases, then the (1,3) prints tuple2 with boolean. I assume there is some type casting going on here, but I’m unclear why.

Can someone give me a quick explanation.
Thanks

  • 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-18T00:18:26+00:00Added an answer on June 18, 2026 at 12:18 am

    Type erasure. It can’t tell what the types are inside the Tuple at runtime. It will compile fine, but it should emit a warning. This is what happens when I do it in :paste mode in the REPL:

    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    def test(x: Any): Unit= x match{
      case i: Int => println("int")
      case b: Boolean => println("bool")
      case ti: (_, Int) => println("tuple2 with int")
      case tb: (_, Boolean)=> println("tuple2 with boolean")
      case _ => println("other")
      }                                                
    
    // Exiting paste mode, now interpreting.
    
    <console>:10: warning: non-variable type argument Int in type pattern (_, Int) is unchecked since it is eliminated by erasure
             case ti: (_, Int) => println("tuple2 with int")
                      ^
    <console>:11: warning: non-variable type argument Boolean in type pattern (_, Boolean) is unchecked since it is eliminated by erasure
             case tb: (_, Boolean)=> println("tuple2 with boolean")
                      ^
    <console>:11: warning: unreachable code
             case tb: (_, Boolean)=> println("tuple2 with boolean")
                                            ^
    test: (x: Any)Unit
    

    Notice the last warning, it says the (_, Boolean) is unreachable because the (_, Int) will match on every Tuple2, courtesy of type erasure.

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

Sidebar

Related Questions

I was going through some data structures and I noticed this as a time
I've been going through some C source code and I noticed the following: void
Hi I am going back through C programming exercises and in this particular program
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore
Been going through some framework classes using reflector and noticed a number of the
I am going through this book and have hit some examples that I am
After going through some examples on the web I realize that there is a
I'm fairly new the functional programming, so I'm going through some practice exercises. I
I was just going through some C code, when I got stuck upon this
So I've been going through some code, and there's some things I can't understand.

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.