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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:57:26+00:00 2026-05-20T18:57:26+00:00

I’ve been diving into scala, and am absolutely loving it so far. I’m getting

  • 0

I’ve been diving into scala, and am absolutely loving it so far. I’m getting around to pattern matching and case classes, and the following has me somewhat stumped. I know what it does, but I want to understand exactly what is happening

var list = List(2,3,4)
1::list

If I am correct in my understanding so far. Then the :: represents a case object. If so my question is:

how am I “left applying” it to the 1? instead of :: being a method of 1. Basically can someone pull this statement 1::list apart somewhat, showing what is really happening (ie. what methods are being called on what object)

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-05-20T18:57:27+00:00Added an answer on May 20, 2026 at 6:57 pm

    It’s annoying that some of the most visible and awesome features of Scala have so much complexity right beneath the surface. So, consider this simple line:

    val (head :: tail): ::[Int] = 1 :: Nil
    

    Each of the three places where :: appears refers to a different ::, and a different mechanism in Scala. Let’s go through each of them, in order.

    head :: tail

    What is happening here is pattern matching, just like one sees with case statements. Pattern matching can appear on val assignments, on the left side of <- in for comprehensions, and on case statements.

    So, how does this particular pattern matching happens? Well, whenever the pattern is in the format a b c, Scala translates this into b(a, c), which is then translated into calls to unapply or unapplySeq on the object b.

    So, :: in val (head :: tail) refers to the object :: (defined through a case class).

    : ::[Int]

    This is a type declaration, so ::[Int] is a type. :: itself is a class, and a type constructor as well (because it constructs types given a type parameter — ::[Int] is one type, ::[String] is another type, etc). It is also a subclass of List, which has only two subclasses: :: and the singleton class of Nil.

    This declaration is superfluous, and, generally speaking, one hardly ever uses :: as a type or class. I show it here mostly for completeness.

    1 :: Nil

    Here, :: is a method. It is a method of List, so, since Nil is a List and 1 is not, it must belong to Nil (or be available through implicit conversion).

    The mechanism of note here is that methods ending with :, when used in infix operator notation, bind to the right instead of to the left. Or, in other words, a :: b is equivalent to b.::(a).

    This mechanism is rarely used and, I suspect, made mostly to make traditional fp list algorithms more familiar to programmers used to fp. It is used in a few other places on Scala standard library and out of it.

    On Scala 2.8, for instance, there’s now +:, which serves the same purpose of ::, but is defined for all Seq. It is mirrored by :+, which appends elements, and whose : serves no purpose other than disambiguate it from +, which is overloaded to concatenate strings.

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

Sidebar

Related Questions

No related questions found

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.