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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:38:09+00:00 2026-05-24T11:38:09+00:00

Encountered a weirdness in Scala (2.8.1) in handling an overloaded method where the first

  • 0

Encountered a weirdness in Scala (2.8.1) in handling an overloaded method where the first is a no-args one and the second takes a variable number of arguments (0..N). Test code:

class Test {
  def method1 = println("method1 invoked with zero args")
  def method1(args: Any*) = println("method1 with args " + args)

  def method2() = println("method2 invoked with zero args")
  def method2(args: Any*) = println("method2 with args " + args)
}

object Test {
  def main(args: Array[String]) {
    val t = new Test
    t.method1
    t.method1()
    t.method1(1,2,3)
    println
    t.method2
    t.method2()
    t.method2(1,2,3)
  }
}

By compiling & running it, the output is:

method1 invoked with zero args
method1 with args WrappedArray()
method1 with args WrappedArray(1, 2, 3)

method2 invoked with zero args
method2 invoked with zero args
method2 with args WrappedArray(1, 2, 3)

So if running method1 with parenthesis and zero arguments we get to the varargs method, but in method2‘s case the no-args method is invoked.

What is the explanation or reasoning behind this weird behavior?

  • 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-24T11:38:10+00:00Added an answer on May 24, 2026 at 11:38 am

    Well, method1() cannot be the def method = declaration, since it is not possible to call a method without a parameter list passing a parameter list.

    scala> def method1 = 0
    method1: Int
    
    scala> method1()
    <console>:9: error: Int does not take parameters
                  method1()
                         ^
    

    Note that the error comes from Scala trying to call apply() on the result of method1, which is an Int.

    Conversely, one cannot call a vararg method with a parameter list.

    scala> def vararg(x: Int*) = x.size
    vararg: (x: Int*)Int
    
    scala> vararg
    <console>:9: error: missing arguments for method vararg;
    follow this method with `_' if you want to treat it as a partially applied function
                  vararg
                  ^
    

    Therefore, in the first case, there’s no other possible behavior than what was shown.

    In the second example, the first and last cases are non-ambiguous. One can’t call a vararg without a parameter list (as shown), and one can’t call a method without parameters passing a parameter. One can call a method with an empty parameter list without parameters, which was done mostly to make Java APIs more “pretty” — for example, .toString.

    The second case — calling the method with an empty parameter list — introduces some ambiguity. Scala then tries to determine if one is more specific than the other.

    Let’s take a brief detour here. Why check for more specific methods? Suppose you have this:

    object T {
      def f(x: AnyRef) = x.hashCode
      def f(x: String) = x.length
    }
    

    This kind of thing is relatively common on Java APIs. If someone called f("abc"), one naturally expects the compiler to call the second method, not the first, but both are valid. So, how to disambiguate them? Perhaps, since String is a perfect match for what is being passed, one might use that, right? Well, consider, then, this:

    object T {
      def f(x: AnyRef) = x.hashCode
      def f(x: java.util.Collection[_]) = x.size
    }
    

    And I call it with f(new ArrayList[String]) So, what now? Well, AnyRef is a class, while Collection is an interface, so we might give precedence to interfaces over classes? And what if I had another method expecting List — that’s also an interface.

    So, to solve this ambiguity, Scala uses the most specific concept. That is actually a pretty easy concept to apply.

    First, Scala verifies is one is as specific as the other. There are four simple rules defined in terms of types and expressions, but the gist of it is that method a is as specific as method b if b can be called with a’s parameters.

    In this case, method2() is as specific as method2(args: Any*), because method2(args: Any*) can be called with an empty parameter list. On the other hand, method2(args: Any*) is not as specific as method2(), because method2() cannot be called with arguments of type Any.

    Next, Scala verifies if one the class that defines one of the methods is a subclass of the class that defines the other. This rule does not apply for this case.

    Finally, Scala adds 1 to each method for each of the two criteria above it fits. So method2() has weight 1, and method2(args: Any*) has weight 0. If the weight of one is greater than the other, that method is considered to be the most specific.

    As we saw, method2() is the most specific one, so it is chosen.

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

Sidebar

Related Questions

I encountered a code where this() method in java takes three parameters two being
I encountered a problem after having more than one texture in my engine. My
I encountered the method o.propertyIsEnumerable(x) in Javascript code. I understand it as a synonym
I encountered a problem here. I'm using C++ multiset. This is the test file.
I encountered a never ending Loading dialog when moving from one page to the
I encountered an issue using jQuery's .delegate() method on IE8 when removing the elements
Issue encountered in .net framework 4.0 I have 2 dropdownlist currently with one for
I encountered a strange problem today. Whenever i put a breakpoint in one of
I encountered an assignment question giving me String test = problemnumber3; System.out.println(test.charAt(1)); System.out.println(test.charAt(7)); String
Encountered a frustrating problem in our application today which came down to an ArrayIndexOutOfBounds

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.