Scala · OOP & Functional Programming

Extension Methods

Extension methods add new methods to types you didn’t write, making code read naturally.

Advanced 5 min Lesson 19 of 25Examples use Scala 3 (3.3 LTS or newer) with sbt or scala-cli.
Before you start:Given and Using
0/25 in Scala
On this page · 5 min · Advanced

In simple words#

An extension method is a phone cover with a built-in stand: you add a feature to the phone without opening it up or changing how it was made.

extension
Defines methods for an existing type.
Receiver
The value the method is called on.
Import scope
Extensions work where they are visible.

Learning objectives#

  • Write extension methods
  • Group related extensions
  • Combine extensions with type classes
  • Control visibility with imports

Explanation#

extension (s: String) def isEmail: Boolean = s.contains("@") lets you write "a@b.com".isEmail.

Group several methods: extension (n: Int) { def rupees … ; def lakhs … }.

Extensions can be generic and require type classes: extension [A](a: A)(using Show[A]) def show = ….

Define them in objects and import where needed to avoid clutter.

Code example: Rupee formatting and validation#

ScalaRupee formatting and validation Illustrative
object syntax:
  extension (amount: Double)
    def rupees: String = f"₹$amount%,.2f"
    def withGst: Double = amount * 1.18


  extension (n: Int)
    def lakh: Int = n * 100000


  extension (s: String)
    def isValidPin: Boolean = s.length == 6 && s.forall(_.isDigit)


import syntax.*


println(1499.0.withGst.rupees)
println(5.lakh)
println("411001".isValidPin)
println("4110".isValidPin)
Expected output
₹1,768.82
500000
true
false

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Extensions are grouped in object syntax.
  2. import syntax.* brings them into scope.
  3. Calls read like built-in methods.
  4. Original types are unchanged.

Real-world use case#

Teams create small DSLs: 5.lakh, order.isOverdue, "text".slugify — readable and consistent.

Common mistakes#

Defining extensions everywhere

Collect them in one object per domain and import explicitly.

Clashing with existing method names

Real members win over extensions; choose distinct names.

Best practices#

  • Group in objects
  • Import explicitly
  • Keep them small
  • Use for readability, not hidden logic

Remember this#

extension (x: T) def m = …

Import to use.

Adds methods without changing the type.

Practice exercise#

Your task: Add an extension method initials to String ("Asha Verma" → "AV").

Show solution
Scala Illustrative
extension (s: String)
  def initials: String = s.split(" ").map(_.head).mkString

Mini quiz#

Extension Methods quizTrue or false Question 1 of 1

Extension methods modify the original class definition.

Score so far: 0/0

Want more? Practise all Scala MCQs · Scala interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.