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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:33:18+00:00 2026-05-26T04:33:18+00:00

The Scala compiler currently cannot infer return types of recursive methods as in the

  • 0

The Scala compiler currently cannot infer return types of recursive methods as in the following code

def foo(i:Int) = if (i > 0) foo (i-1) else 0

Is there any ambuigity in the above statement? (i.e., is any type other than Int possible?)

I can imagine that in more complex example, it will be difficult to infer the type.

Is it possible to further characterize the cases of recursive methods where we can(not) infer the types?

[EDIT:] The compiler is intelligent enough to figure out that String is incorrect.

scala> def foo(i:Int):String = if (i > 0) foo (i-1) else 0
<console>:5: error: type mismatch;
found   : Int(0)
required: String
  • 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-26T04:33:19+00:00Added an answer on May 26, 2026 at 4:33 am

    If your recursive call is always in the last position, i.e. its value is never used and only returned, it should be possible to determine the type as the common supertype of all other branches.

    However, in a situation like

    def foo(i: Int) = if (i > 0) foo(i - 1) + 1 else 0
    

    you would not know the type of foo(i - 1) + 1 (or understand the operation + because it is actually a method on foo – things are getting more complicated in the presence of objects) without knowing what foo is. So, again you’re moving around in circles.

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

Sidebar

Related Questions

Using Scala's command line REPL: def foo(x: Int): Unit = {} def foo(x: String):
Given this Scala code: object test { def byval(a: Int) = println(Int) def byval(a:
When I execute the following code in scala REPL: javax.persistence.Persistence.createEntityManagerFactory(manager1) I get javax.persistence.PersistenceException: No
I've got a simple question for you :) I have the following Scala code,
Why doesn't scalac (the Scala compiler) optimize tail recursion? Code and compiler invocations that
The Scala compiler compiles direct to Java byte code (or .NET CIL). Some of
In my quest to generate new code in a Scala compiler plugin, I have
scala> val l = List((1,2), (2,3)) l: List[(Int, Int)] = List((1,2), (2,3)) I can
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
Learning Scala currently and needed to invert a Map to do some inverted value->key

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.