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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:11:52+00:00 2026-05-28T01:11:52+00:00

I defined a method treeNode to create a node, and which can have children

  • 0

I defined a method treeNode to create a node, and which can have children nodes. The simplified code is:

def treeNode(text:String) (children: => Any) {
    val b = new TreeNode(text)
    children
}

When I use this method, I have to write:

treeNode("aaa") {
    treeNode("bbb") {}
    treeNode("ccc") {}
}

You can see the leaf nodes they don’t have children, but they have to have a empty block {}.

Is there any way to give the parameter children: => Any a default do-nothing value, that I can write the code as:

treeNode("aaa") {
    treeNode("bbb")
    treeNode("ccc")
}

Help~

  • 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-28T01:11:53+00:00Added an answer on May 28, 2026 at 1:11 am

    The problem is not that you can’t give it a do-nothing (default) value; the problem is that even if you do, functions with multiple parameter blocks have to at least have parentheses or braces for each block.

    There is one exception: an implicit parameter block does not need to be referenced at all. Unfortunately, you’re not allowed to have call-by-name implicit parameters, and even if you were, your signature would allow any random implicit to work in that spot!

    Now, there is a way around this, which I will show for completeness, but I suggest that (assuming you don’t just want another name, like leafNode) you just leave the trailing {} there.

    You can get exactly the syntax that you want if you do the following. First, you need an implicit parameter, but you make it a wrapper class (could use Function0 which already exists, but then the next step might have unintended consequences):

    trait AnyByName { def eval: Any }
    def treeNode(text: String)(implicit children: AnyByName) = (text,children.eval)
    

    Now you need two things–you need to be able to convert a by-name Any into your new trait, and you need to have an implicit do-nothing one available. So we

    implicit val nameForDoingNothing = new AnyByName { def eval = () }
    implicit def wrap_any_by_name(a: => Any) = new AnyByName { def eval = a }
    

    And now we recover the behavior that you were after:

    scala> treeNode("Hi")
    res1: (String, Any) = (Hi,())
    
    scala> treeNode("Hi") { treeNode("there") }
    res2: (String, Any) = (Hi,(there,()))
    

    (in your example, you don’t return anything; here I do, to show that it works.)

    It’s a lot of tooling just to avoid some {}s, though, which is why I’d suggest only doing this if you anticipate this to be a very heavily used DSL and that two names is unacceptable. (Also, if you expect it to be very heavily used, treeNode is probably painfully long as a name; I’d suggest just node.)

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

Sidebar

Related Questions

I have a method defined in ApplicationController : def some_checking ... end And I
i have a code like this i have defined the method goNext but still
I have defined a method called ccy which takes in a number num ,
I'm trying to create an App in Django, which requires a pre-defined method to
I have GetURL method defined to find my location from network provider, you can
For example, I defined a method: def hello(String name) { println(Hello + name) }
I have a method defined in my controller that I am trying to create
I have a method defined in IDL as follows : interface IMyFunc : IDispatch
Here's my problem: I have a virtual method defined in a .h file that
I have written a code that solves MST using Prim method. I read that

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.