E174: Inline Given Should Not Be Function — inline given의 우변은 함수가 아니어야 함

E174: Inline Given Should Not Be Function — inline given의 우변은 함수가 아니어야 함

inline given 별칭의 우변이 함수 값일 때 발생하는 경고예요. 함수가 사용되는 모든 지점에서 인라인되면서 생성되는 코드 크기가 크게 늘어날 수 있기 때문이에요.

출처: Scala 3 Reference

본문

inline을 함수를 반환하는 given과 함께 쓰면, 함수 리터럴이 given을 summon하는 모든 곳에서 복제돼서 코드가 비대해져요.

예시 (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
}

더 알아보기

함수 값이 우변에 오는 inline giveninline을 떼거나, 명시적인 apply 메서드로 다시 쓰는 걸 권장해요.