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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:44:08+00:00 2026-06-03T18:44:08+00:00

I think this will be better explained with an example I have the following

  • 0

I think this will be better explained with an example

I have the following case class

case class Person(name: String = "no name", surname: String = "no surname")

And I want to make a general function to populate it from, for example, a json message, that might not specify all fields

I know that to use the default values the simple answer is not to pass them to the constructor, but if I have several fields that may or may not appear in the json, I should have to use a huge switch sentence covering every possible combination of missing parameters. In this case, after reading the json, I should take care of name & surname present, no name, no surname and no name nor surname case… (Gee, I hope I made myself clear).

To be more precise, I’m trying to develop a function that allows me to create a person from the following json values, using the default values when there’s some parameter missing

{ "name": "john", "surname": "doe" }
{ "surname": "doe" }
{ "name": "john" }
{ }

That’s why I’m looking for a more general way to handle this.

(I’ll show some pseudo code to give and idea of what I’m trying to achieve)

I was thinking about something like:

val p = Person(name= "new person name", surname= Unit)

And in that case surname should get the default value

Or something like

val p = Person( Map( "name" -> "new person name" ) _* )

So that it also takes the default value for surname

Or maybe doing it in the constructor, if I detect a null value (or None) I could assign the default value.

In fact, I’m trying to avoid repeating the definition of the default values.

Anyway, what would be the most idiomatic way to achieve such a thing?

  • 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-03T18:44:09+00:00Added an answer on June 3, 2026 at 6:44 pm

    If you want the default value to be used, you normally just leave off that named argument:

    scala> val p = Person(name = "new person name")
    p: Person = Person(new person name,no surname)
    

    But, since you want to explicitly know whether a value should be defaulted or not, you could implement your Map-based idea in a constructor. If you don’t want to repeat the defaults, how about these two options:

    Option 1: Externalized constants for defaults

    Set the defaults externally. Use them in both the main constructor and the Map-based constructor.

    val nameDefault = "no name"
    val surnameDefault = "no surname"
    
    case class Person(name: String = nameDefault, surname: String = surnameDefault) {
      def this(m: Map[String, String]) =
        this(m.getOrElse("name", nameDefault), m.getOrElse("surname", surnameDefault))
    }
    

    Usage:

    new Person(name = "new person name", surname = "new person surname")
    new Person(Map("name" -> "new person name"))
    new Person(name = "new person name")
    

    Option 2: Optionified alternate constructor

    You may find this a little cleaner since it doesn’t rely on externalized constants. The only downside here is that if you want to construct with only some of the parameters, you have to wrap each one in Some().

    case class Person(name: String, surname: String) {
      def this(name: Option[String] = None, surname: Option[String] = None) =
        this(name.getOrElse("no name"), surname.getOrElse("no surname"))
    
      def this(m: Map[String, String]) = this(m.get("name"), m.get("surname"))
    }
    

    Usage:

    new Person(name = "new person name", surname = "new person surname")
    new Person(Map("name" -> "new person name"))
    new Person(name = Some("new person name"))
    new Person(name = "new person name") // can't do this
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think my question is best explained via an example: Suppose I want to
Do you think that C# will support something like ??= operator? Instead of this:
I have this thing that i need to do and some advices will be
I want to use an example to explain what I want. Assume I've following
I'm wondering if this kind of thing will ever be possible in PHP (and
I think this is likely to be a generic .NET assembly loading question, but
I THINK this line is causing a 3.5K Byte memory leak, any ideas why?
I think this is the question, anyway. I am using a RelayCommand, which decorates
I think this question may end up being a bit subjective so I'm marking
I think this is an escaping issue or something. When I execute the query

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.