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

  • Home
  • SEARCH
  • 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 6200021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:17:33+00:00 2026-05-24T04:17:33+00:00

I have some Scala code roughly analogous to this: object Foo { val thingA

  • 0

I have some Scala code roughly analogous to this:

object Foo {
    val thingA = ...
    val thingB = ...
    val thingC = ...
    val thingD = ...
    val thingE = ...

    val thingsOfAKind = List(thingA, thingC, thingE)
    val thingsOfADifferentKind = List(thingB, thingD)
    val allThings = thingsOfAKind ::: thingsOfADifferentKind
}

Is there some nicer way of declaring a bunch of things and being able to access them both individually by name and collectively?

The issue I have with the code above is that the real version has almost 30 different things, and there’s no way to actually ensure that each new thing I add is also added to an appropriate list (or that allThings doesn’t end up with duplicates, although that’s relatively easy to fix).

The various things are able to be treated in aggregate by almost all of the code base, but there are a few places and a couple of things where the individual identity matters.

I thought about just using a Map, but then the compiler looses the ability to check that the individual things being looked up actually exist (and I have to either wrap code to handle a failed lookup around every attempt, or ignore the problem and effectively risk null pointer exceptions).

I could make the kind each thing belongs to an observable property of the things, then I would at least have a single list of all things and could get lists of each of the kinds with filter, but the core issue remains that I would ideally like to be able to declare that a thing exists, has a name (identifier), and is part of a collection.

What I effectively want is something like a compile-time Map. Is there a good way of achieving something like this in Scala?

  • 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-24T04:17:33+00:00Added an answer on May 24, 2026 at 4:17 am

    How about this type of pattern?

    class Things[A] {
      var all: List[A] = Nil
      def ->: (x: A): A = { all = x :: all; x }
    }
    
    object Test {
      val things1 = new Things[String]
      val things2 = new Things[String]
    
      val thingA = "A" ->: things1
      val thingB = "B" ->: things2
      val thingC = "C" ->: things1
      val thingD = ("D" ->: things1) ->: things2
    }
    

    You could also add a little sugar, making Things automatically convertible to List,

    object Things {
      implicit def thingsToList[A](things: Things[A]): List[A] = things.all
    }
    

    I can’t think of a way to do this without the var that has equally nice syntax.

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

Sidebar

Related Questions

Say I have some Scala code like this: // Outputs 1, 4, 9, 16,
I have a servlet coded in Scala. I have some code like this in
I have some code like this in a winforms app I was writing to
I have some Scala code that does something nifty with two different versions of
I have some mutable scala code that I am trying to rewrite in a
Let's say I have this Hello.scala. object HelloWorld { def main(args: Array[String]) { println(Hello,
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some classes layed out like this class A { public virtual void
I have some C# / asp.net code I inherited which has a textbox which
In some library code, I have a List that can contain 50,000 items or

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.