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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:31:16+00:00 2026-05-23T13:31:16+00:00

This is more a design question than anything else… I really like Scala’s case

  • 0

This is more a design question than anything else…

I really like Scala’s case classes and use them often. However, I find that I’m often wrapping in my parameters in Options (or rather, Lift’s Boxes) and setting default values to allow for flexibility and to account that a user might not always specify all the parameters. I think I adopted this practice from.

My question is, is this a reasonable approach? Given that everything may be optional, there can be a lot of boilerplate and checking, to the point whether I wonder whether I’m not just using my case classes like Map[String, Any] and wonder whether I wouldn’t be better off just using a Map.

Let me give you a real example. Here I am modeling a money transfer:

case class Amount(amount: Double, currency: Box[Currency] = Empty)
trait TransactionSide
case class From(amount: Box[Amount] = Empty, currency: Box[Currency] = Empty, country: Box[Country] = Empty) extends TransactionSide
case class To(amount: Box[Amount] = Empty, currency: Box[Currency] = Empty, country: Box[Country] = Empty) extends TransactionSide
case class Transaction(from: From, to: To)

Relatively simple to understand, I think. At this simplest we might declare a Transaction like so:

val t = Transaction(From(amount=Full(Amount(100.0)), To(country=Full(US)))

Already I can imagine you think it’s verbose. And if we specify everything:

val t2 = Transaction(From(Full(Amount(100.0, Full(EUR))), Full(EUR), Full(Netherlands)), To(Full(Amount(150.0, Full(USD))), Full(USD), Full(US)))

On the other hand, despite having to throw Full around everywhere, you can still do some nice pattern matching:

t2 match {
  case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(country_from)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(country_to))) if country_from == country_to => Failure("You're trying to transfer to the same country!")
  case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(US)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(North_Korea))) => Failure("Transfers from the US to North Korea are not allowed!")
  case Transaction(From(Full(Amount(amount_from, Full(currency_from1))), Full(currency_from2), Full(country_from)), To(Full(Amount(amount_to, Full(currency_to1))), Full(currency_to2), Full(country_to))) => Full([something])
  case _ => Empty
}

Is this a reasonable approach? Would I be better served by using a Map? Or should I use case classes but in a different fashion? Perhaps using a whole hierarchy of case classes to represent transactions with different amounts of information specified?

  • 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-23T13:31:16+00:00Added an answer on May 23, 2026 at 1:31 pm

    Using a case class is less flexible than a map, since you can only assign/access pre-defined fields. You will need to build a full case class hierarchy beforehand.

    On the other hand, case class offer kind of “compile-time validations”, because all the types are explicitly defined (in contrast to a Map[String,Any]), and you cannot assign/access a non specified field by mistake. Case classes should also be faster, because you don’t need to traverse the map hashtable to find what you are looking for.

    The “verbosity” problem comes from the immutable aspect of case classes, but you’ll have exactly the same problem with immutable maps. The solution seems to be Lenses. There is a very nice talk here:

    http://www.youtube.com/watch?v=efv0SQNde5Q&list=PLEDE5BE0C69AF6CCE

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

Sidebar

Related Questions

So this question is more around the design decision than anything else.Essientially, I want
This is more of a philosophical question than anything, so give me your thoughts.
This is more like a design question but related to SQL optimization as well.
This is more of an academic inquiry than a practical question. Are there any
This is more an observation than a real question: MS-Access (and VBA in general)
Ok this is more of a computer science question, than a question based on
EDIT: This question is more about language engineering than C++ itself. I used C++
This is a question about style and design rather than syntax. I have domain
This is a design question rather than a Java-specific question, but I'm designing it
This question is more towards Design and Architecture and I want to know SO

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.