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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:08:14+00:00 2026-05-15T19:08:14+00:00

I have a case class case class ~[a,b](_1:a, _2:b) When I want to do

  • 0

I have a case class

case class ~[a,b](_1:a, _2:b)

When I want to do pattetn matching

new ~("a", 25) match{
  case "a" ~ 25 =>
}

I can use it this way because "a" ~ 25 and ~("a", 25) are equivalent. But if I want to match new ~("a", new ~("b", 25)) by {case "a" ~ "b" ~ 25 => } troubles begin. I understand that this statements aren’t equivalent. So, how new ~("a", new ~("b", 25)) can be presented? By what rules?

  • 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-15T19:08:14+00:00Added an answer on May 15, 2026 at 7:08 pm

    This works:

    new ~("a", new ~("b", 25)) match {
      case "a" ~ ("b" ~ 25) =>
    }
    

    So, you’ll have to put the parentheses the same way they are in the initial clause. Otherwise the tilde is left-associative, therefore the type of the pattern will be different and it won’t compile.

    case "a" ~ "b" ~ 25
    

    is the same as

    case ("a" ~ "b") ~ 25
    

    which would be wrong in your case.

    Appendix

    You can get right-associativity by having a colon as the last character in your class/method name. The following compiles and matches without parentheses (and you can drop the new as the compiler won’t be confused anymore by $tilde$colon):

    case class ~:[a,b](_1:a, _2:b)
    
    ~:("a", ~:("b", 25)) match {
      case "a" ~: "b" ~: 25 =>
    }
    

    Responses

    1) Without the new keyword, the case class ~ is shadowed by unary_~ which gives the bitwise negation of the argument. An expression like ~ 2 is internally evaluated to unary_~(2) and the same goes for the case of ~ ("a", 1) – however unless you define unary_~ on that tuple, this will give an error. With the new keyword you advise the compiler to explicitly look for a class with that name, so it won’t get confused.
    (Technically, you could work around this by using $tilde("a", 1) which is the internal name for your case class ~, but since this is a compiler detail, you should probably not rely on it.)

    2&3) The right-associativity is referenced in the Scala Language Specification. Section ‘Infix Operations’

    The associativity of an operator is determined by the operator’s last character. Operators ending in a colon ‘:’ are right-associative. All other operators are left-associative.

    By the way, right-associativity is the trick that allows for creating lists with Nil. (Nil is the empty List and has the right-associative concatenation operator :: defined.)

    val l: List[Int] = 1 :: 2 :: 3 :: Nil
    

    which is evaluated as follows

    val l: List[Int] = (1 :: (2 :: (3 :: Nil)))
    

    or, more precisely, since 3 :: Nil ≡ Nil.::(3), as

    val l: List[Int] = ( ( Nil.::(3) ).::(2) ).::(1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Have a use case wherein need to maintain a connection open to a database
I have a case where a 3rd party ad is bleeding through my modal
I have a case that keeps coming up where I'm using a ListView or
I have a case where if a SharePoint site owner decides to break permissions
I have a case where the child view sends notification to its parent view.
I have a case where i'm getting formatted text with <a href ...> ,
I have a case where I need to translate (lookup) several values from the
I have a case where I need to serve raw swf files to the
I am trying to understand if I really have any case for using git/mercurial.

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.