E197: Inlined Anonymous Class Warning

E197: Inlined Anonymous Class Warning

이 경고는 인라인(inline) 메서드가 익명 클래스를 만들 때 발생해요. 인라인 메서드는 각 호출 지점에서 확장되기 때문에 익명 클래스 정의가 생성된 바이트코드에 중복되고, 결국 클래스 파일이 많이 늘어날 수 있어요.

출처: Scala 3 Reference

본문

예시 (Example)

inline def createObject(): Object =
  new Object {}

에러 (Error)

-- [E197] Potential Issue Warning: example.scala:2:2 ---------------------------
2 |  new Object {}
  |  ^
  |  New anonymous class definition will be duplicated at each inline site
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Anonymous class will be defined at each use site, which may lead to a larger number of classfiles.
  |
  | To inline class definitions, you may provide an explicit class name to avoid this warning.
   -----------------------------------------------------------------------------

해결 방법 (Solution)

인라인 메서드 안에서 이름이 있는 클래스를 사용해요.

inline def createObject(): Object =
  class NamedClass extends Object
  new NamedClass

아니면 클래스를 인라인 메서드 바깥에 정의해도 돼요.

class MyObject extends Object

inline def createObject(): Object =
  new MyObject