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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:19:10+00:00 2026-05-28T11:19:10+00:00

I need to model a node in a tree. Node has children, of course,

  • 0

I need to model a node in a tree. Node has children, of course, and there is inheritance hierarchy of nodes:

trait Node extends Seq[Node] { 
  val children:List[Node] = List()
  def apply(n:Int) = children(n)
  def iterator = children.iterator
  def length = children.length
  def add(n:Node):Node = { ??? }
}
case class NodeA(n:Int) extends Node
case class NodeB(n:String) extends Node
case class NodeC(c:String) extends NodeB(c)

val na:NodeA = new NodeA(1)
val na1:NodeA = na.add(new NodeB("a"))
na1.children == List(NodeB("a"))
val na2:NodeA = na1.add(new NodeC("b"))
na2.children == List(NodeB("a"), NodeC("b"))

The problem here is that I want to keep Node and its subclasses immutable.

That’s then I begin having problems. How do I write method .add on Node properly? Because obviously calling NodeA.add should return NodeA, not Node.
The collateral damage is that even if I put all Node-specific methods implementations (children, add, remove) into each of subclasses (or into companion objects, builders, CanBuildFrom’s, etc), then I’ll need to define children as a constructor parameter. Which would clutter Node subclasses definition and usage – I should avoid it if possible.

Is this possible? Or am I doing something wrong and there is another way?

  • 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-28T11:19:10+00:00Added an answer on May 28, 2026 at 11:19 am

    Are you asking how to abstract over the return type of add? If so, you might want to look into functional dependencies. See also CanBuildFrom from the Scala collections, as described here and here.

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

Sidebar

Related Questions

I need help with a problem regarding data saved in a parent-children model table
I've a tree-like polymorphic data-structure, where the nodes are instances of class Node (implemented
Is there a good way to expand/close all the expandable nodes in a dijit.Tree
I'm working on a Web service in Django, and I need to model a
I have a need for a model(?) on my app which basically contains a
i have very simple problem. I need to create model, that represent element of
I need to load a model, existing of +/- 20 tables from the database
I need to create a 3D model of a cube with a circular hole
I need to store some data in a Django model. These data are not
Both my Rails model and controller code need to write files to the 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.