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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:44:25+00:00 2026-05-25T18:44:25+00:00

Is it possible to map the key value pairs of a Map to a

  • 0

Is it possible to map the key value pairs of a Map to a Scala constructor with named parameters?

That is, given

class Person(val firstname: String, val lastname: String) {
    ...
}

… how can I create an instance of Person using a map like

val args = Map("firstname" -> "John", "lastname" -> "Doe", "ignored" -> "value")

What I am trying to achieve in the end is a nice way of mapping Node4J Node objects to Scala value objects.

  • 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-25T18:44:26+00:00Added an answer on May 25, 2026 at 6:44 pm

    The key insight here is that the constructor arguments names are available, as they are the names of the fields created by the constructor. So provided that the constructor does nothing with its arguments but assign them to fields, then we can ignore it and work with the fields directly.

    We can use:

    def setFields[A](o : A, values: Map[String, Any]): A = {
      for ((name, value) <- values) setField(o, name, value)
      o
    }
    
    def setField(o: Any, fieldName: String, fieldValue: Any) {
      // TODO - look up the class hierarchy for superclass fields
      o.getClass.getDeclaredFields.find( _.getName == fieldName) match {
        case Some(field) => {
          field.setAccessible(true)
          field.set(o, fieldValue)
        }
        case None =>
          throw new IllegalArgumentException("No field named " + fieldName)
      }
    

    Which we can call on a blank person:

    test("test setFields") {
      val p = setFields(new Person(null, null, -1), Map("firstname" -> "Duncan", "lastname" -> "McGregor", "age" -> 44))
      p.firstname should be ("Duncan")
      p.lastname should be ("McGregor")
      p.age should be (44)
    }
    

    Of course we can do better with a little pimping:

    implicit def any2WithFields[A](o: A) = new AnyRef {
      def withFields(values: Map[String, Any]): A = setFields(o, values)
      def withFields(values: Pair[String, Any]*): A = withFields(Map(values :_*))
    }
    

    so that you can call:

    new Person(null, null, -1).withFields("firstname" -> "Duncan", "lastname" -> "McGregor", "age" -> 44)
    

    If having to call the constructor is annoying, Objenesis lets you ignore the lack of a no-arg constructor:

    val objensis = new ObjenesisStd 
    
    def create[A](implicit m: scala.reflect.Manifest[A]): A = 
      objensis.newInstance(m.erasure).asInstanceOf[A]
    

    Now we can combine the two to write

    create[Person].withFields("firstname" -> "Duncan", "lastname" -> "McGregor", "age" -> 44)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Here's
I'm passing in a bunch of key-value pairs as parameters to a XSL (date
Is there any open-source document-oriented key-value map/reduce storage that: is easily embeddable (Yes, it
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? Dear,
Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? I
Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic According
Rails make It possible to map resources that are logically children of other resources
I have a 'black box' application that gets a map of values as parameters,
Problem I need a key-value store that can store values of the following form:
Is this possible? #include <map> class Example { private: std::map<std::string, std::string, less<std::string>, std::allocator< CustomPair<std::string,

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.