Extension Methods
Add methods to types.
Extension Methods is a free Scala for Backend Engineering & Functional Programming lesson on CoddyKit — lesson 4 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.
Adding methods to existing types
Extension methods let you add new methods to a type you do not own, like Int or String, without modifying its source. Scala 3 has clean syntax for this.
object Main:
extension (x: Int)
def squared: Int = x * x
def main(args: Array[String]): Unit =
println(5.squared)The extension syntax
Write extension (param: Type) followed by one or more method definitions. Inside each method, the parameter refers to the receiver value.
object Main:
extension (s: String)
def shout: String = s.toUpperCase + "!"
def main(args: Array[String]): Unit =
println("hello".shout)Multiple methods in one block
A single extension clause can define several methods that all operate on the same receiver type.
object Main:
extension (x: Int)
def squared: Int = x * x
def cubed: Int = x * x * x
def isEven: Boolean = x % 2 == 0
def main(args: Array[String]): Unit =
println(4.squared)
println(3.cubed)
println(6.isEven)Extension methods with parameters
Extension methods can take their own parameters in addition to the receiver.
object Main:
extension (x: Int)
def times(s: String): String = s * x
def main(args: Array[String]): Unit =
println(3.times("ab"))Generic extension methods
Extensions can be generic, adding methods to a whole family of types like List[A].
object Main:
extension [A](xs: List[A])
def second: A = xs(1)
def main(args: Array[String]): Unit =
println(List(10, 20, 30).second)
println(List("a", "b").second)Why extensions beat implicit conversions
Extension methods achieve the old 'pimp-my-library' goal more safely: they only add the named methods, with no risk of silent type conversions elsewhere.
object Main:
extension (d: Double)
def percentOf(total: Double): Double = d / total * 100
def main(args: Array[String]): Unit =
println(25.0.percentOf(200.0))Extensions and type classes
Extension methods pair beautifully with type classes: define behavior via a given instance and expose it as a method using using.
trait Show[A]:
def show(a: A): String
object Main:
given Show[Int] with
def show(a: Int): String = s"<$a>"
extension [A](a: A)
def display(using s: Show[A]): String = s.show(a)
def main(args: Array[String]): Unit =
println(42.display)Defining operators
Because operators are just methods, extensions can add symbolic operators to existing types too.
object Main:
extension (x: Int)
def **(exp: Int): Int = math.pow(x, exp).toInt
def main(args: Array[String]): Unit =
println(2 ** 10)Scoping and imports
Extension methods are only available where they are in scope. Define them in an object and import it to use them, keeping their reach controlled.
object StringOps:
extension (s: String)
def reversedWords: String = s.split(" ").reverse.mkString(" ")
object Main:
import StringOps._
def main(args: Array[String]): Unit =
println("hello there world".reversedWords)Chaining extension methods
Extension methods return ordinary values, so you can chain them with built-in methods to form fluent pipelines.
object Main:
extension (s: String)
def emphasize: String = s"*$s*"
def main(args: Array[String]): Unit =
println("scala".toUpperCase.emphasize)A practical helper
Extension methods are perfect for small domain helpers that make calling code read naturally.
object Main:
extension (n: Int)
def isPrime: Boolean =
n > 1 && (2 until n).forall(d => n % d != 0)
def main(args: Array[String]): Unit =
println((2 to 12).filter(_.isPrime).toList)Quick Check
What is the main advantage of Scala 3 extension methods over implicit conversions for adding methods?
Recap
You learned extension methods:
extension (x: Type) def ...adds methods to existing types- One block can define multiple methods, including operators
- They can be generic and take parameters
- They pair naturally with type classes via
using - Safer than implicit conversions and must be imported into scope
Frequently asked questions
Is the “Extension Methods” lesson free?
Yes — the full text of “Extension 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 “Extension Methods”?
Add methods to types. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Extension 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
- Implicit Parameters
- Scala 3 given/using
- Implicit Conversions
- Extension Methods