inlineable 안에서는 return을 쓸 수 없어요
inlineable 안에서는 return을 쓸 수 없어요 (E090: No Return From Inlineable)
inline 메서드 안에 명시적인 return 문이 있으면 이 에러가 나요. inline 수식어가 붙은 메서드에서는 return을 쓸 수 없어요.
본문
inline 메서드 안에 명시적인 return 문이 들어 있으면 이 에러가 발생해요.
inline 수식어가 붙은 메서드에서는 return 문을 사용할 수 없어요. 메서드가 인라인되면 return이 기대한 대로 동작하지 않을 수 있거든요. 대신 마지막 표현식의 값이 반환되도록 구조를 짜는 게 맞아요.
예시 (Example)
inline def example(x: Int): Int =
if x < 0 then return 0
x * 2
에러 (Error)
-- [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)
return 대신 if-else 표현식을 사용해요.
// Use if-else expression instead of return
inline def example(x: Int): Int =
if x < 0 then 0
else x * 2
return이 꼭 필요하다면 inline을 빼면 돼요.
// Or remove inline if you need return
def example(x: Int): Int =
if x < 0 then return 0
x * 2