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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:55:56+00:00 2026-05-15T12:55:56+00:00

The Scala compiler can often infer return types for methods, but there are some

  • 0

The Scala compiler can often infer return types for methods, but there are some circumstances where it’s required to specify the return type. Recursive methods, for example, require a return type to be specified.

I notice that sometimes I get the error message “overloaded method (methodname) requires return type”, but it’s not a general rule that return types must always be specified for overloaded methods (I have examples where I don’t get this error).

When exactly is it required to specify a return type, for methods in general and specifically for overloaded methods?

  • 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-15T12:55:57+00:00Added an answer on May 15, 2026 at 12:55 pm

    The Chapter 2. Type Less, Do More of the Programming Scala book mentions:

    When Explicit Type Annotations Are Required.

    In practical terms, you have to provide explicit type annotations for the following situations:

    Method return values in the following cases:

    • When you explicitly call return in a method (even at the end).
    • When a method is recursive.
    • When a method is overloaded and one of the methods calls another. The calling method needs a return type annotation.
    • When the inferred return type would be more general than you intended, e.g., Any.

    Example:

    // code-examples/TypeLessDoMore/method-nested-return-script.scala
    // ERROR: Won't compile until you put a String return type on upCase.
    
    def upCase(s: String) = {
      if (s.length == 0)
        return s    // ERROR - forces return type of upCase to be declared.
      else
        s.toUpperCase()
    }
    

    Overloaded methods can sometimes require an explicit return type. When one such method calls another, we have to add a return type to the one doing the calling, as in this example.

    // code-examples/TypeLessDoMore/method-overloaded-return-script.scala
    // Version 1 of "StringUtil" (with a compilation error).
    // ERROR: Won't compile: needs a String return type on the second "joiner".
    
    object StringUtil {
      def joiner(strings: List[String], separator: String): String =
        strings.mkString(separator)
    
      def joiner(strings: List[String]) = joiner(strings, " ")   // ERROR
    }
    import StringUtil._  // Import the joiner methods.
    
    println( joiner(List("Programming", "Scala")) )
    

    The two joiner methods concatenate a List of strings together.
    The first method also takes an argument for the separator string.
    The second method calls the first with a “default” separator of a single space.

    If you run this script, you get the following error.

    ... 9: error: overloaded method joiner needs result type
    def joiner(strings: List[String]) = joiner(strings, "")
    

    Since the second joiner method calls the first, it requires an explicit String return type. It should look like this:

    def joiner(strings: List[String]): String = joiner(strings, " ")
    

    Basically, specifying the return type can be a good practice even though Scala can infer it.


    Randall Schulz comments:

    As a matter of (my personal) style, I give explicit return types for all but the most simple methods (basically, one-liners with no conditional logic).

    Keep in mind that if you let the compiler infer a method’s result type, it may well be more specific than you want. (E.g., HashMap instead of Map.)

    And since you may want to expose the minimal interface in your return type (see for instance this SO question), this kind of inference might get in the way.


    And about the last scenario (“When the inferred return type would be more general than you intended”), Ken Bloom adds:

    specify the return type when you want the compiler to verify that code in the function returns the type you expected

    (The faulty code which triggers a “more general than expected return type was:

    // code-examples/TypeLessDoMore/method-broad-inference-return-script.scala
    // ERROR: Won't compile. Method actually returns List[Any], which is too "broad".
    
    def makeList(strings: String*) = {
      if (strings.length == 0)
        List(0)  // #1
      else
        strings.toList
    }
    
    val list: List[String] = makeList()  // ERROR
    

    , which I incorrectly interpreted and List[Any] because returning an empty List, but Ken called it out:

    List(0) doesn’t create a list with 0 elements.
    It creates a List[Int] containing one element (the value 0).
    Thus a List[Int] on one conditional branch and a List[String] on the other conditional branch generalize to List[Any].
    In this case, the typer isn’t being overly-general — it’s a bug in the code.
    )

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

Sidebar

Related Questions

The Scala compiler currently cannot infer return types of recursive methods as in the
Often there's no need to pay any attention to implicit arguments in Scala, but
How can you access scala.None from Java? The last line causes the compiler to
How can I make sed filter matching lines according to some expression, but ignore
What techniques can I use in Scala to deal with long type parameter lists?
According to Programming in Scala one can pass the argument -Xprint:typer to the compiler
The Scala compiler compiles direct to Java byte code (or .NET CIL). Some of
There are many ways functions can be defined in Scala, which leads to confusion
I have seen some nice Java compiler hacks wherein you can replace assertions by
Following Scala mailing lists, different people often say: compiler rewrites this [scala] code into

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.