Back to Scala3

E090: No Return From Inlineable

docs/_docs/reference/error-codes/E090.md

3.8.41.7 KB
Original Source

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

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

Error

scala
-- [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

scala
// Use if-else expression instead of return
inline def example(x: Int): Int =
  if x < 0 then 0
  else x * 2
scala
// Or remove inline if you need return
def example(x: Int): Int =
  if x < 0 then return 0
  x * 2
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>