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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:22:12+00:00 2026-06-03T13:22:12+00:00

I have a domain model that looks like this: case class Account(id: Int, name:

  • 0

I have a domain model that looks like this:

case class Account(id: Int, name: String)

trait BalanceTotal {
  def balance: BigDecimal
}

My goal is to have a simple, lightweight Account case class and then an enhanced class Account with BalanceTotal that will only be created inside a method that does the balance calculation (which is expensive).

With this structure, I can statically ensure that I’m never counting on a balance being present when I just have a lightweight object. I know about Option, of course, but I want the type-checker to prevent code from using the lightweight object where I need the balance-enriched object, and vice-versa, and I don’t think Option gives me that.

Anyway, what I’d like to do is:

for{ Account(id,name) <- accounts } {
  yield Account(id, name) with BalanceTotal {
    override val balance = BigDecimal(44)
  }
}

but that fails with:

';' expected but 'with' found

If I try to use new Account instead of Account, I get:

super constructor arguments cannot reference unconstructed `this`

which I assume has something to do with the voodoo that makes case classes work.

Is it possible to do what I’m trying to do?

Update: On further consideration, I guess this has something to do with the prohibition against case class inheritance. But I really do need case class magic here: I’m relying on the generated apply and unapply methods for my ScalaQuery mappings.

  • 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-03T13:22:13+00:00Added an answer on June 3, 2026 at 1:22 pm

    It works OK for me:

    >scala
    Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.
    7.0).
    Type in expressions to have them evaluated.
    Type :help for more information.
    
    scala> :paste
    // Entering paste mode (ctrl-D to finish)
    
    case class Account(id: Int, name: String)
    
    trait BalanceTotal {
      def balance: BigDecimal
    }
    val enrichedAccount = new Account(1, "foo") with BalanceTotal {
      override val balance = BigDecimal(44)
    }
    
    // Exiting paste mode, now interpreting.
    
    defined class Account
    defined trait BalanceTotal
    enrichedAccount: Account with BalanceTotal = Account(1,foo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class in my domain model root that looks like this: namespace
I have a model that looks like this: public interface IEntity { int Id
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have a model named Domain which looks like this: class Domain(models.Model): Model for
I have a domain model that looks like this: Instruction | \ Money Other
I have a Domain Model for Countries and States that looks like the following(see
I have a form that handles editing of a domain model (A). This domain
Let's say i have a Domain Model that im trying to make compatible with
I have two tables that are considered a single entity in my domain model.
I currently have a client that wants me to 'abstract' out a domain model

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.