Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Kotlin for Enterprise Applications using Java EE

You're reading from   Kotlin for Enterprise Applications using Java EE Develop, test, and troubleshoot enterprise applications and microservices with Kotlin and Java EE

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781788997270
Length 388 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Raghavendra Rao K Raghavendra Rao K
Author Profile Icon Raghavendra Rao K
Raghavendra Rao K
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Kotlin – A First look FREE CHAPTER 2. Kotlin – The Game Changer 3. An Overview of Java EE and Kotlin 4. Kotlin with JSF and CDI 5. Kotlin with JPA and EJB 6. Enterprise Messaging with Kotlin 7. Developing RESTful Services with JAX-RS 8. Securing JAVA EE Applications with Kotlin 9. Implementing Microservices with Kotlin 10. Performance Monitoring and Logging 11. Design Patterns with Kotlin 12. Other Books You May Enjoy

Extension functions

In Kotlin, we can easily add functionality to existing classes through its extension functions. The language provides the ability to add additional functions to classes without modifying the source code. In this section, we will look at how to extend the functionality of classes so that they provide the exact functionality that we are interested in. Kotlin supports extension functions and extension properties.

To create an extension function, we start with the fun keyword, and then we prefix the name of the extension function with the name of the class to be extended (known as the receiver type), followed by the dot operator (represented by the . character).

An example of this is receiverType.customFunctionName().

Consider the following code for 13_ExtensionFunctons.kts:

fun String.myExtendedFunction() = toUpperCase()

println("Kotlin".myExtendedFunction())

Here, myExtendedFunction() is an extension to the existing String class. We added myExtendedFunction() as an extension function and invoked it on a String literal.

The output for the preceding example is as follows:

This can be pictorially represented as follows:

  • Extension functions do not get added to the original classes. Instead, new functions are made callable by adding a dot on the receiverType variables.
  • Extension functions are resolved during compilation time. They are statically typed. This means that the extension function being invoked is determined by the receiverType of the expression on which the function is invoked, not by the nature of the result of evaluating the expression at runtime.
  • The extension functions cannot be overloaded.
You have been reading a chapter from
Kotlin for Enterprise Applications using Java EE
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781788997270
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime