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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:29:16+00:00 2026-06-12T06:29:16+00:00

Is it possible to build an Immutable List through inheritance in Scala? I have

  • 0

Is it possible to build an Immutable List through inheritance in Scala? I have a base trait which creates a MenuBar. The MenuBar will have the standard headers: File, Edit, View / Window, Help etc. I want inheriting traits to be able to add sub-items to the menu headings. I want the Menus to be created dynamically, so there will be a list of functions which will be called when the menu is opened to create the sub tree. As the list of functions will be known at compile time it would be nice if it could be an immutable list.

Is there any way in Scala to create an immutable list through a constructor/ initialisation hierarchy? If not in Scala has any language offered this feature?

To illustrate the problem with a simple example using a mutable list. TrA and TrB are written independently of each other:

trait Base
{
  val list = scala.collection.mutable.LinkedList[String]()
}
trait TrA extends Base
{
  list += "A"
}
trait TrB extends Base
{
  list += "B"
}

val ab = new TrA with TrB {}

As the contents of the List are known at compile time is there anyway to make it a val immutable list? Of course any mutable collection can be made public as an immutable collectiion through a def call.

The following will compile:

trait Base
{ val list: List[String] = Nil }

trait TrA extends Base
{ val list = "A" :: super.list }

trait TrB extends Base
{ val list = "B" :: super.list }

val ab = new TrA with TrB {}

But it can’t be initialised without throwing a null exception. Making val list lazy doesn’t help either, hence the necessity for 0__ ‘s solution.

  • 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-12T06:29:18+00:00Added an answer on June 12, 2026 at 6:29 am

    This is very similar to an older question I remember; I couldn’t find it anymore, so I tried to reconstruct the approach:

    trait Base {
      protected def contribute : List[String] = Nil
    
      val list = contribute
    }
    
    trait TrA extends Base {
       override protected def contribute = "A" :: super.contribute
    }
    
    trait TrB extends Base {
       override protected def contribute = "B" :: super.contribute
    }
    
    val x = new TrA with TrB {}
    x.list  // List(B, A)
    val y = new TrB with TrA {}
    y.list  // List(A, B)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to build an iOS app that will get user's voicemails ?
If I have 6 modules in my project is it possible to build only
I am curious if it is possible to dynamically build a list comprehension in
Is it possible to build msi file using installshield, so that I have one
Is it possible to build a regexp to match all keywords in a scala
Is it possible to build a Java web application which has a PHP front
Is is possible to build a MSI installer which can do a per-user install
Is it possible to build a Class Library dll which also includes WPF forms?
Is it possible to build a DataTable object from scratch using F# I have
I was wondering if it's possible to build an app which works as a

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.