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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:23:37+00:00 2026-05-20T12:23:37+00:00

Have a look at the following Scala example: class A { def foo(x: Int):

  • 0

Have a look at the following Scala example:

class A {
  def foo(x: Int): Int = x

  private class B {
    def foo(): Int = foo(3)
  }
}

The compiler produces an error message when trying to compile this:

A.scala:5: error: too many arguments for method foo: ()Int
                def foo(): Int = foo(3)
                                    ^

For some reason the compiler doesn’t look in the enclosing class A to find the method to call. It only looks in class B, finds the foo method there that takes no parameters that doesn’t fit and then gives up. If I rename the methods, then it works without a problem:

class A {
  def bar(x: Int): Int = x

  private class B {
    def foo(): Int = bar(3)
  }
}

In this case, the compiler does look in class A and finds the bar method there.

Why does the first example not work; is this according to Scala’s specifications, or is this a compiler bug? If this is according to the rules, then why are the rules like this?

By the way, another way to get around the problem is by using a self type annotation:

class A {
  self =>

  def foo(x: Int): Int = x

  private class B {
    def foo(): Int = self.foo(3)
  }
}
  • 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-20T12:23:37+00:00Added an answer on May 20, 2026 at 12:23 pm

    Technically the class B is a block. You could reduce the problem to the following:

    def foo(x: Int): Int = x;
    {    
       def foo(): Int = foo(3)
    }
    

    That would cause the exact same problem. It is compliant to the specs, because all names introduced in a block shadow anything that has the same name (ignoring the signature, see chapter 2 of the spec). Overloading is only possible on class level. (chapter 6.26.3 in the spec)

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

Sidebar

Related Questions

Have a look at following scenario: public class ParentClass { private Integer testVar =
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Say you define the following: class Person(name: String, age: Int) { def toXml =
Have a look at the following piece of code Header File: class CxUser {
Have a look at the following code snippet... File1.h void somefunc(int); File1.c #include File1.h
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import
Have a look at the following code you are not required to read the
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I

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.