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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:24:27+00:00 2026-06-15T06:24:27+00:00

I have a tree object that implements lazy depth-first-search as a TraversableView . import

  • 0

I have a tree object that implements lazy depth-first-search as a TraversableView.

import collection.TraversableView

case class Node[T](label: T, ns: Node[T]*)

case class Tree[T](root: Node[T]) extends TraversableView[T, Traversable[_]] {
    protected def underlying = null

    def foreach[U](f: (T) => U) {
        def dfs(r: Node[T]): TraversableView[T, Traversable[_]] = {
            Traversable(r.label).view ++ r.ns.flatMap(dfs(_))
        }
        dfs(root).foreach(f)
    }
}

This is appealingly concise and appears to work; however, the underlying = null method makes me nervous because I don’t understand what it means. (IntelliJ wrote that line for me.) I suppose it might be correct, because in this case there is no underlying strict representation of the tree, but I’m not sure.

Is the above code correct, or do I have to do something more with underlying?

  • 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-15T06:24:28+00:00Added an answer on June 15, 2026 at 6:24 am

    Users of views will expect to be able to call force to get a strict collection. With your implementation, calling force on a tree (or any transformation of a tree—e.g., tree.take(10).filter(pred), etc.) will result in a null pointer exception.

    This may be fine with you—you’ll still be able to force evaluation using toList, for example (although you should follow the advice in DaoWen’s comment if you go that route).

    The actual contents of underlying should never get used, though, so there’s an easy fix—just make it an appropriately typed empty collection:

    protected def underlying = Vector.empty[T]
    

    Now if a user calls tree.force, they’ll get a vector of labels, statically typed as a Traversable[T].

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

Sidebar

Related Questions

I have a Binary Search Tree, I use template to add any class object
I have a business object project, which contains composite structure: public class Tree {
I have an own Tree object implemented in PHP. Say we have a tree
I have a tree-like object, managed by Hibernate. The object has a list of
I have tree like following example, where every leaf is object. [1] |--[2] |
I am trying to have a javascript object tree behave like a php associative
In Lua, I have a tree relationship structure between objects where an object can
Alright, so I've been trying to implement a simple binary search tree that uses
Ok, so I have a class for a Advanced Data Structure (in this case
I have VTreePanel class that extends from CPanel which extends from JPanel. The class

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.