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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:12:55+00:00 2026-06-12T09:12:55+00:00

I am trying to create a class that takes in a function, however, I

  • 0

I am trying to create a class that takes in a function, however, I want this to be optional and if the caller leaves it out, I want it to be substituted with a default function.

Currently I have the following:

class MenuItem(val text: String, val onClick: (Vector2f) => Unit) extends Renderable {
  def this(text: String) = this(text, { position => () })

  private def default(position: Vector2f) = {
    println(text + " was clicked, but is still using the default event.")
  }
}

This works fine but I want to replace the { position => () }) in the constructor with default but doing this results in the error ‘not found: value default’.

I’ve tried declaring default without the private modifier but this didn’t help.

The best solution I came up with was to declare it like this:

def this(text: String) = this(text, position => println(text + " was clicked, but is still using the default event."))

Is there a way I can leave the default method defined and just use its name or will I have to stick to implementing the method inline?

  • 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-12T09:12:56+00:00Added an answer on June 12, 2026 at 9:12 am

    This should work (removed the weird types to make it compilable):

    object MenuItem {
        def default(text: String, position: AnyRef) = {
            println(text + " was clicked, but is still using the default event.")
        }
    }
    
    class MenuItem(val text: String, val onClick: (AnyRef) => Unit) {
        def this(text: String) = this(text, MenuItem.default(text, _))
    }
    

    Accessing a method of the object you are trying to construct doesn’t work because it is only accessible once it is constructed.

    From the Language Specification Section 5.3.1:

    The signature and the self constructor invocation of a constructor
    definition are type-checked and evaluated in the scope which is in
    effect at the point of the enclosing class definition, augmented by any
    type parameters of the enclosing class and by any early definitions
    (§5.1.6) of the enclosing template.

    The ‘self constructor invocation is the call to the primary constructor. And since it is type checked in the scope of the enclosing class means it does not see the methods inside the class.

    This is a good thing, because before the primary constructor is called, the fields of the instance (text in this case) aren’t defined.

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

Sidebar

Related Questions

I'm trying to figure out the best way to create a class that can
I'm trying to create a function that takes any table from my ADO.NET Data
I'm trying to create a template function that takes a vector argument of any
I'm trying to create a class that inherits from ListBox and adding the property
I'm trying to create a class that manages the events of a combo box
I am trying to create a class that implements the IUnknown interface. I have
I'm trying to create a class that can instantiate arrays at runtime by giving
i am trying to create a class that will show the contents of a
I am trying to create a new class Dog that inherits via prototypical inheritance
I'm trying to create a custom javascript class that inherits from google.maps.Map (V3 API).

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.