E044: Overloaded Or Recursive Method Needs Result Type

This error is emitted when an overloaded or recursive method is defined without an explicit return type.

Case 1: Overloaded methods - If there are multiple methods with the same name and at least one definition calls another, you need to specify the calling method's return type.

Case 2: Recursive methods - If a method calls itself on any path (even through mutual recursion), you need to specify the return type of that method or of a definition it's mutually recursive with.


Example

def factorial(n: Int) =
  if n <= 1 then 1
  else n * factorial(n - 1)

Error

-- [E044] Cyclic Error: example.scala:3:11 -------------------------------------
3 |  else n * factorial(n - 1)
  |           ^
  |Overloaded or recursive method factorial needs return type
  |
  |The error occurred while trying to compute the signature of method factorial
  |  which required to type the right hand side of method factorial since no explicit type was given
  |  which required to compute the signature of method factorial
  |
  | Run with both -explain-cyclic and -Ydebug-cyclic to see full stack trace.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Case 1: method factorial is overloaded
  | If there are multiple methods named method factorial and at least one definition of
  | it calls another, you need to specify the calling method's return type.
  |
  | Case 2: method factorial is recursive
  | If method factorial calls itself on any path (even through mutual recursion), you need to specify the return type
  | of method factorial or of a definition it's mutually recursive with.
   -----------------------------------------------------------------------------

Solution

// Add an explicit return type
def factorial(n: Int): Int =
  if n <= 1 then 1
  else n * factorial(n - 1)
// For mutually recursive methods, at least one needs a return type
def isEven(n: Int): Boolean =
  if n == 0 then true else isOdd(n - 1)

def isOdd(n: Int): Boolean =
  if n == 0 then false else isEven(n - 1)