인라인 메서드
Scala 3 inline을 알아봅니다
인라인 메서드은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
What Is inline?
The inline keyword in Scala 3 tells the compiler to expand a definition at the call site during compilation. It is the entry point to metaprogramming, before full macros.
An Inline Method
Mark a method inline and its body is substituted wherever it is called. This can remove call overhead and enable compile-time specialization.
inline def square(x: Int): Int = x * x
val r = square(5) // compiles to 5 * 5Inline Parameters
An inline parameter forces its argument to be inlined too. If the argument is a constant, the compiler can fold it into a literal.
inline def power(x: Int, inline n: Int): Int =
if (n == 0) 1 else x * power(x, n - 1)Constant Folding
When inline parameters are literals, branches and recursion can be fully resolved at compile time, leaving only the final expression in the bytecode.
inline def power(x: Int, inline n: Int): Int =
if (n == 0) 1 else x * power(x, n - 1)
val c = power(2, 3) // expands to 2 * 2 * 2 * 1inline if and inline match
Inside an inline method, inline if and inline match are resolved at compile time when the scrutinee is statically known, eliminating dead branches.
inline def label(inline b: Boolean): String =
inline if (b) "yes" else "no"Transparent Inline
transparent inline lets the method's return type be refined per call site — the compiler may infer a more specific type than the declared one.
transparent inline def first(inline b: Boolean): Any =
inline if (b) 1 else "x" // type can be Int or Stringcompiletime.error
The scala.compiletime package offers tools like error to fail compilation with a custom message — useful for validating inline arguments.
import scala.compiletime.error
inline def positive(inline n: Int): Int =
inline if (n <= 0) error("n must be positive") else nInline Values
An inline val is a true compile-time constant. It can be used where literals are required, such as in other inline computations.
inline val MaxRetries = 3
inline def retries: Int = MaxRetriesPerformance vs Code Size
Inlining trades code size for speed and compile-time guarantees. Overusing it on large bodies bloats bytecode, so reserve it for small, hot, or specialization-critical methods.
Runnable Without Inline
Here is the same squaring logic as an ordinary self-contained program. The inline version would produce identical results with the multiplication expanded at the call site.
object Main {
def square(x: Int): Int = x * x
def main(args: Array[String]): Unit = {
println(square(5)) // 25
}
}Inline Is the Foundation
Macros build on inline methods: an inline def can call a macro to generate code. Mastering inline first makes the macro layer much easier.
Quick Check
What does adding transparent to an inline method change?
Recap
You learned Scala 3 inlining:
inline defexpands at the call siteinlineparameters and constant foldinginline if/inline matchtransparent inline,inline val,compiletime.error
Next: macro basics.
자주 묻는 질문
“인라인 메서드” 강의는 무료인가요?
네 — “인라인 메서드” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
“인라인 메서드”에서 뭘 배우나요?
Scala 3 inline을 알아봅니다 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.
“인라인 메서드” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.