inlineable 안에서는 super 호출을 쓸 수 없어요

inlineable 안에서는 super 호출을 쓸 수 없어요 (E082: Super Calls Not Allowed In Inlineable)

inline 메서드 안에서 super를 호출하면 이 에러가 나요. 메서드 인라인 처리에서는 수퍼클래스 메서드 호출이 금지돼요.

출처: Scala 3 Reference

본문

inline 메서드 안에 super 호출이 들어 있으면 이 에러가 발생해요.

메서드 인라인 처리에서는 수퍼클래스 메서드를 호출할 수 없어요. 메서드가 호출 지점에서 인라인되면 super 참조가 모호해지거나 무효해질 수 있거든요. 더 이상 정의 클래스의 수퍼클래스를 가리키지 않게 되죠.

예시 (Example)

class Parent:
  def greet: String = "Hello"

class Child extends Parent:
  inline def greetLoud: String = super.greet + "!"

에러 (Error)

-- [E082] Syntax Error: example.scala:5:33 -------------------------------------
5 |  inline def greetLoud: String = super.greet + "!"
  |                                 ^^^^^
  |                     Super call not allowed in inlineable method greetLoud
  |-----------------------------------------------------------------------------
  | Explanation (enabled by `-explain`)
  |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  | Method inlining prohibits calling superclass methods, as it may lead to confusion about which super is being called.
   -----------------------------------------------------------------------------

해결 방법 (Solution)

inline 수식어를 빼면 돼요.

// Remove the inline modifier
class Parent:
  def greet: String = "Hello"

class Child extends Parent:
  def greetLoud: String = super.greet + "!"

또는 super 호출을 헬퍼 메서드로 옮겨두고, 인라인 메서드는 그 헬퍼를 호출하게 하는 방법도 있어요.

// Or use a helper method for the super call
class Parent:
  def greet: String = "Hello"

class Child extends Parent:
  private def parentGreet: String = super.greet
  inline def greetLoud: String = parentGreet + "!"