0Pricing
Scala for Backend Engineering & Functional Programming · Lesson

Inline Methods

Scala 3 inline.

Inline Methods is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Scala for Backend Engineering & Functional Programming learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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 * 5

Inline 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 * 1

inline 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 String

compiletime.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 n

Inline 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 = MaxRetries

Performance 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 def expands at the call site
  • inline parameters and constant folding
  • inline if / inline match
  • transparent inline, inline val, compiletime.error

Next: macro basics.

Frequently asked questions

Is the “Inline Methods” lesson free?

Yes — the full text of “Inline Methods” is free to read here on the web, and the Scala for Backend Engineering & Functional Programming course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Scala for Backend Engineering & Functional Programming course, upgrade to CoddyKit PRO.

What will I learn in “Inline Methods”?

Scala 3 inline. You practise Scala for Backend Engineering & Functional Programming with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Scala for Backend Engineering & Functional Programming?

No prior experience is required. Scala for Backend Engineering & Functional Programming on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Inline Methods” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Scala for Backend Engineering & Functional Programming lesson?

Yes. Every Scala for Backend Engineering & Functional Programming lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Inline Methods
  2. Macros Basics
  3. Quotes and Splices
  4. Practical Macros
← Back to Scala for Backend Engineering & Functional Programming