E174: Inline Given Should Not Be Function

This warning occurs when an inline given alias has a function value as its right-hand side. This pattern can significantly increase generated code size because the function is inlined at every use site.

When you combine inline with a given that returns a function, the function literal is duplicated everywhere the given is summoned, leading to code bloat.


Example

inline given toStringFunc: (Int => String) = (x: Int) => x.toString

Error

-- [E174] Syntax Warning: example.scala:1:54 -----------------------------------
1 |inline given toStringFunc: (Int => String) = (x: Int) => x.toString
  |                                             ^^^^^^^^^^^^^^^^^^^^^^
  |An inline given alias with a function value as right-hand side can significantly increase
  |generated code size. You should either drop the `inline` or rewrite the given with an
  |explicit `apply` method.
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | A function value on the right-hand side of an inline given alias expands to
  | an anonymous class. Each application of the inline given will then create a
  | fresh copy of that class, which can increase code size in surprising ways.
  | For that reason, functions are discouraged as right hand sides of inline given aliases.
  | You should either drop `inline` or rewrite to an explicit `apply` method. E.g.
  |
  |     inline given Conversion[A, B] = x => x.toB
  |
  | should be re-formulated as
  |
  |     given Conversion[A, B] with
  |       inline def apply(x: A) = x.toB
  |
   -----------------------------------------------------------------------------

Solution

// Option 1: Remove the inline modifier
given toStringFunc: (Int => String) = (x: Int) => x.toString
// Option 2: Use an explicit apply method
inline given toStringFunc: (Int => String) with {
  def apply(x: Int): String = x.toString
}