E019: Missing Return Type

This error is emitted when an abstract method declaration is missing its return type. Abstract declarations must have explicit return types. Without a body, the compiler cannot infer the type of the method, so it must be explicitly specified.


Example

trait Foo:
  def bar

Error

-- [E019] Syntax Error: example.scala:2:9 --------------------------------------
2 |  def bar
  |         ^
  |         Missing return type
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | An abstract declaration must have a return type. For example:
  |
  | trait Shape:
  |   def area: Double // abstract declaration returning a Double
   -----------------------------------------------------------------------------

Solution

// Add an explicit return type
trait Foo:
  def bar: Unit
// Or provide an implementation (then type can be inferred)
trait Foo:
  def bar = println("hello")
// For methods with parameters
trait Calculator:
  def add(a: Int, b: Int): Int
  def multiply(a: Int, b: Int): Int