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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:27:33+00:00 2026-05-23T15:27:33+00:00

Possible Duplicate: Scala map containing mix type values I have a situation where in

  • 0

Possible Duplicate:
Scala map containing mix type values

I have a situation where in a previous groovy program is constructing a structure like

mp = [k1: "string", k2: [d1: [1,2,3], d2: 1975], k3: 345, k4: ["one","two"]]

which amounts to:

[String: String, String: Map[String, Any], String: Int, String: List[String]]

The function returns mp to the calling function.

as you can see the values of mp map are irregular. I am rewriting the program in scala.

In scala I have to represent mp as Map[String,Any] but this results playing the dance with isInstanceOf and asInstanceOf in the test code that makes use of mp which results in a lot of scala boilerplate code. The main reason being Scala complains about List[String] cannot be cast to Any for example.

Is there a better solution in scala?

When looking more carefully, this question is different from my previous question. In this question, as you can see the value [d1: [1,2,3], d2: “string”] for key k2 is itself is irregular map.

I was proposing this solution:
Create another scala class like:

class MakeMap  {

// lot of code here
....

val k1: String  = // put here value found in logic above
val k2 : Map[String, Any]  // here is the issue, I am again forced to use Any
val k3 : List[String]  // put here value found in logic above

}

in test code:

val m1 = new MakeMap()
m1.k1  // very easy to access value, also gets rid of scala verbose syntax to access vale
      // in map like getOrElse
m1.k2  // this is the issue, it seesm I have to define yet another class
m1.k3 // again easy
  • 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-23T15:27:33+00:00Added an answer on May 23, 2026 at 3:27 pm

    If you are rewriting a code from Groovy to Scala, I suppose you are looking for performance and safety. So try to avoid using Maps to store everything because you may loose both. Try to define in advance the data structures you need, and use case class which could be seen as immutable structs.

    For instance, an equivalent of the Groovy value:

    [k1: "string", k2: [d1: [1,2,3], d2: 1975], k3: 345, k4: ["one","two"]]
    

    Could be defined as:

    case class Outer( k1: String, k2: Inner, k3: Int, k4: List[String] )
    case class Inner( d1: List[Int], d2: Int )
    

    Then you can create your object mp as:

    val mp = Outer( "string", Inner( List(1,2,3), 1975 ), 345, List("one","two") )
    

    And access members like:

    val x = mp.k2.d2

    Of course, give sensible names. Try first to constrain your types as much as possible, avoiding Any, AnyRef or AnyVal. It’s often possible to do it.

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

Sidebar

Related Questions

Possible Duplicate: How do I setup multiple type bounds in Scala? I'm a little
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Scala traits vs abstract classes What is the conceptual difference between abstract
Possible Duplicate: Accessing scala.None from Java In Java you can create an instance of
Possible Duplicate: Which IDE for Scala 2.8? I'm learning Scala by reading 'Programming in
Possible Duplicate: How to detect if JavaScript is disabled? I have a website which
Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular
Possible Duplicate: val and object inside a scala class? Is there a substantive difference
Possible Duplicate: What's the (hidden) cost of lazy val? (Scala) Scala allows the definition
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file

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.