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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:06:02+00:00 2026-05-26T14:06:02+00:00

Being new to scala and a current java developer, scala was designed to encourage

  • 0

Being new to scala and a current java developer, scala was designed to encourage the use of immutability to class design.

How does this translate practically to the design of classes? The only thing that is brought to my mind is case classes. Are case classes strongly encouraged for defining data? Example? How else is immutability encouraged in Scala design of classes?

As a java developer, classes defining data were mutable. The equivalent Scala classes should be defined as case classes?

  • 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-26T14:06:02+00:00Added an answer on May 26, 2026 at 2:06 pm

    Well, case classes certainly help, but the biggest contributor is probably the collection library. The default collections are immutable, and the methods are geared toward manipulating collections by producing new ones instead of mutating. Since the immutable collections are persistent, that doesn’t require copying the whole collection, which is something one often has to do in Java.

    Beyond that, for-comprehensions are monadic comprehensions, which is helpful in doing immutable tasks, there’s tail recursion optimization, which is very important in immutable algorithms, and general attention to immutability in many libraries, such as parser combinators and xml.

    Finally, note that you have to ask for a var to get some mutability. Parameters are immutable, and val is just as short as var. Contrast this with Java, where parameters are mutable, and you need to add a final keyword to get immutability. Whereas in Scala it is as easy or easier to stay immutable, in Java it is easier to stay mutable.

    Addendum

    Persistent data structures are data structures that share parts between modified versions of it. This might be a bit difficult to understand, so let’s consider Scala’s List, which is pretty basic and easy to understand.

    A Scala List is composed of two classes, known as cons and Nil. The former is actually written :: in Scala, but I’ll refer to it by the traditional name.

    Nil is the empty list. It doesn’t contain anything. Methods that depend on the list not being empty, such as head and tail throw exceptions, while others work ok.

    Naturally, cons must then represent a non-empty list. In fact, cons has exactly two elements: a value, and a list. These elements are known as head and tail.

    So a list with three elements is composed of three cons, since each cons will hold only one value, plus a Nil. It must have a Nil because a cons must point to a list. As lists are not circular, then one of the cons must point to something other than a cons.

    One example of such list is this:

    val list = 1 :: 2 :: 3 :: Nil
    

    Now, the components of a Scala List are immutable. One cannot change neither the value nor the list of a cons. One benefit of immutability is that you never need to copy the collection before passing or after receiving it from some other method: you know that list cannot change.

    Now, let’s consider what would happen if I modified that list. Let’s consider two modifications: removing the first element and prepending a new element.

    We can remove one element with the method tail, whose name is not a coincidence at all. So, we write:

    val list2 = list.tail
    

    And list2 will point to the same list that list‘s tail is pointing. Nothing at all was created: we simply reused part of list. So, let’s prepend an element to list2 then:

    val list3 = 0 :: list2
    

    We created a new cons there. This new cons has a value (a head) equal to 0, and its tail points to list2. Note that both list and list3 point to the same list2. These elements are being shared by both list and list3.

    There are many other persistent data structures. The very fact that the data you are manipulating is immutable makes it easy to share components.

    One can find more information about this subject on the book by Chris Okasaki, Purely Functional Data Structures, or on his freely available thesis by the same name.

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

Sidebar

Related Questions

Being new to test based development, this question has been bugging me. How much
being new to WPF this is a complex problem for me. What i want
I figured this would be simple, but being new to SSRS I'm not sure.
Being fairly new to JavaScript, I'm unable to discern when to use each of
Being relatively new to functional programming, I expend lots of energy wondering is this
Being new to Scala (2.9.1), I have a List[Event] and would like to copy
Being new to Cocoa, and probably not knowing all of the potential classes available
Being new to VS, how may I think of these two concepts, what is
Being new to both WPF and MVVM, I'm studying Josh Smith's article on the
Being relatively new to the .net game, I was wondering, has anyone had any

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.