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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:53:06+00:00 2026-05-30T16:53:06+00:00

Suppose I want to create trees of a certain set depth, that is, the

  • 0

Suppose I want to create trees of a certain set depth, that is, the path length from the top of the tree to any leaf node is some fixed number. Ideally, the type checker would be able to verify that you are creating and using these trees correctly. For my problem, I implemented something like:

import collection.mutable.HashMap

abstract class TreeNode[A, B] {
  def insert(data: B, path: List[A])
}

class TwigNode[A, B] extends TreeNode[A, B] {
  val hm = new HashMap[A, B]

  def insert(data: B, path: List[A]) {
    hm(path.head) = data
  }
}

class BranchNode[A, B](depth: Int) extends TreeNode[A, B] {
  val hm = new HashMap[A, TreeNode[A, B]].withDefaultValue(
    if (depth == 2)
      new TwigNode[A, B]
    else
      new BranchNode[A, B](depth - 1)
    )

  def insert(data: B, path: List[A]) {
    hm(path.head).insert(data, path.tail)
  }
}

But the type checker isn’t helping me here. If there is a bug in the insert method (or any other method) the tree could end up with leaf nodes at different distances. Is it possible to get the type checker to verify everything is correct, with resorting to something crazy (implementing Peano arithmetic in the type system?) or having ugly types like BranchNode[BranchNode[BranchNode[BranchNode[TwigNode[A]]]]]?

  • 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-30T16:53:07+00:00Added an answer on May 30, 2026 at 4:53 pm

    The feature you want is often referred to as a dependent type system. And at the moment there is no common used programming language with that feature implemented.
    Though you can generate more or less practical dependent type systems in C++, they will look somewhat similar to BranchNode[BranchNode[BranchNode[BranchNode[TwigNode[A]]]]]

    So nothing but those ugly things that you have already considered are practical in Scala.

    Although there are some mathematical methods to build and handle complete trees. But they are to be omitted because your disinterest on those is predictable.

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

Sidebar

Related Questions

Suppose I want to create a set of observers based on type. That is
Suppose I want to put objects that identify a server into a stl set
Suppose I want to create, in the spirit of /dev/zero, a file /dev/seven that
Suppose you want to create a parent-child relationship in a table of users that
Suppose that I want to create an SQL SELECT statement dynamically with reflection on
I want create a web application that display a list of items. Suppose I
Suppose I’m making an Objective-C class that represents a fraction, and want to create
Suppose i want to create a method that takes a number as a string
Suppose I want to create a control in Java that is like the Symbol
I want to check if a file path is in current directory tree. Suppose

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.