E090: No Return From Inlineable

This error is emitted when an inline method contains an explicit return statement.

Methods marked with inline modifier may not use return statements because when the method is inlined, the return would not behave as expected. Instead, rely on the last expression's value being returned.


Example

inline def example(x: Int): Int =
  if x < 0 then return 0
  x * 2

Error

-- [E090] Syntax Error: example.scala:2:16 -------------------------------------
2 |  if x < 0 then return 0
  |                ^^^^^^^^
  |                No explicit return allowed from inlineable method example
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Methods marked with inline modifier may not use return statements.
  | Instead, you should rely on the last expression's value being
  | returned from a method.
   -----------------------------------------------------------------------------

Solution

// Use if-else expression instead of return
inline def example(x: Int): Int =
  if x < 0 then 0
  else x * 2
// Or remove inline if you need return
def example(x: Int): Int =
  if x < 0 then return 0
  x * 2