Skip to content

Using Extension Functions in Kotlin

Kotlin, Extension Functions2 min read

An extension function is a function that is defined outside of a class but can be called on an instance of that class. This means that you can add new behavior to an existing class without having to modify the class's source code. Extension functions are defined using the fun keyword, followed by the class name and a dot, and then the name of the function. The receiver type is specified before the function name and is the class that the function is extending.

Here is an example of an extension function that adds a new method to the String class:

1fun String.isEmailValid(): Boolean {
2 val emailPattern = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\$".toRegex()
3 return this.matches(emailPattern)

In this example, the extension function is called isEmailValid(). The function takes no arguments and returns a boolean. The receiver type is String, which means that the function can be called on any instance of the String class. The function uses a regular expression pattern to determine if the string is a valid email address.

How to Use Extension Functions

Using an extension function is easy. You simply call the function on an instance of the class that it is extending. Here is an example of how to use the isEmailValid() extension function from the previous example:

1val email = ""
2if (email.isEmailValid()) {
3 println("$email is a valid email address")
4} else {
5 println("$email is not a valid email address")

In this example, the isEmailValid() function is called on the email string. The function returns a boolean, which is used in an if statement to determine if the email is valid or not.

Advantages of Extension Functions

Extension functions have several advantages over other ways of adding functionality to existing classes. Here are some of the advantages:

  1. No Need to Modify the Original Class: With extension functions, you don't need to modify the source code of the original class. This means that you can add new functionality without affecting the behavior of other parts of your code that use the original class.

  2. Improved Readability: Extension functions can make your code more readable by allowing you to call a function on an object instead of passing the object as an argument to a function. This can result in code that is easier to read and understand.

  3. Easy to Reuse: Extension functions can be reused in different parts of your code. Once you define an extension function, you can use it on any instance of the class that it is extending, without having to define the function again.


Extension functions are a powerful feature of Kotlin that allow you to add new functionality to existing classes without having to modify the original source code. This can improve the readability and maintainability of your code, as well as make it easier to reuse code in different parts of your project. When used effectively, extension functions can help you write more concise, readable, and maintainable code.