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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:56:18+00:00 2026-06-18T16:56:18+00:00

Functional programming promotes immutable classes and referential transparency. Domain-driven design is composed of Value

  • 0

Functional programming promotes immutable classes and referential transparency.

Domain-driven design is composed of Value Object (immutable) and Entities (mutable).

Should we create immutable Entities instead of mutable ones?

Let’s assume, project uses Scala as main language, how could we write Entities as case classes (immutable so) without risking stale status if we’re dealing with concurrency?

What is a good practice? Keeping Entities mutable (var fields etc…) and avoiding great syntax of 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-06-18T16:56:20+00:00Added an answer on June 18, 2026 at 4:56 pm

    You can effectively use immutable Entities in Scala and avoid the horror of mutable fields and all the bugs that derives from mutable state. Using Immutable entities help you with concurrency, doesn’t make things worse. Your previous mutable state will become a set of transformation which will create a new reference at each change.

    At a certain level of your application, however, you will need to have a mutable state, or your application would be useless. The idea is to push it as up as you can in your program logic. Let’s take an example of a Bank Account, which can change because of interest rate and ATM withdrawal or
    deposit.

    You have two valid approaches:

    • You expose methods that can modify an internal property and you manage concurrency on those methods (very few, in fact)

    • You make all the class immutable and you surround it with a “manager” that can change the account.

    Since the first is pretty straightforward, I will detail the first.

    case class BankAccount(val balance:Double, val code:Int)
    
    class BankAccountRef(private var bankAccount:BankAccount){
       def withdraw(withdrawal) = {
           bankAccount = bankAccount.copy(balance = bankAccount.balance - withdrawal)
           bankAccount.balance
       }
    }
    

    This is nice, but gosh, you are still stuck with managing concurrency. Well, Scala offers you a solution for that. The problem here is that if you share your reference to BankAccountRef to your Background job, then you will have to synchronize the call. The problem is that you are doing concurrency in a suboptimal way.

    The optimal way of doing concurrency: message passing

    What if on the other side, the different jobs cannot invoke methods directly on the BankAccount or a BankAccountRef, but just notify them that some operations needs to be performed? Well, then you have an Actor, the favourite way of doing concurrency in Scala.

    class BankAccountActor(private var bankAccount:BankAccount) extends Actor {
    
        def receive {
            case BalanceRequest => sender ! Balance(bankAccount.balance)
            case Withdraw(amount) => {
                this.bankAccount = bankAccount.copy(balance = bankAccount.balance - amount)
            }
            case Deposit(amount) => {
                this.bankAccount = bankAccount.copy(balance = bankAccount.balance + amount)
    
            }
    
        }
    }
    

    This solution is extensively described in Akka documentation: http://doc.akka.io/docs/akka/2.1.0/scala/actors.html . The idea is that you communicate with an Actor by sending messages to its mailbox, and those messages are processed in order of receival. As such, you will never have concurrency flaws if using this model.

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

Sidebar

Related Questions

Pure functional programming languages do not allow mutable data, but some computations are more
I have an idea for a functional programming language design that makes heavy use
All of the collection classes have two versions - mutable and immutable, such as
What languages are available that promote both object-oriented and functional programming? I know that
In functional programming, functions are regarded as entities, and can be passed around as
While studying for a Functional Programming exam, I came across the following questions from
While studying for a Functional Programming exam, I came across the following question from
In the context of functional programming which is the correct term to use: persistent
I'm reading about functional programming and I've noticed that Pattern Matching is mentioned in
I have been learning functional programming and I come to idea, to assemble mathematical

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.