Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Kotlin Standard Library Cookbook

You're reading from   Kotlin Standard Library Cookbook Master the powerful Kotlin standard library through practical code examples

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781788837668
Length 242 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Urbanowicz Urbanowicz
Author Profile Icon Urbanowicz
Urbanowicz
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Title Page
Copyright and Credits
Dedication
Packt Upsell
Contributors
Preface
1. Ranges, Progressions, and Sequences FREE CHAPTER 2. Expressive Functions and Adjustable Interfaces 3. Shaping Code with Kotlin Functional Programming Features 4. Powerful Data Processing 5. Tasteful Design Patterns Adopting Kotlin Concepts 6. Friendly I/O Operations 7. Making Asynchronous Programming Great Again 8. Best Practices for the Android, JUnit, and JVM UI Frameworks 9. Miscellaneous 1. Other Books You May Enjoy Index

Adding custom names for imports


In this recipe, we are going to explore how to add custom names to the import declarations. We are going to import the java.lang.StringBuilder class, add a custom name to it and make use of it in the sample code to demonstrate it in action. 

 

 

 

 

 

 

How to do it...

  1. Import the StringBuilder class with a custom alias:
import java.lang.StringBuilder as builder
  1. Use the custom StringBuilder name in the sample code:
import java.lang.StringBuilder as builder

fun main(vararg args: String) {
val text = builder()
            .append("Code is like humor. ")
            .append("When you have to explain it, ")
            .append("it’s bad.")
            .toString()
println(text)
}

How it works...

As you can see, we were able to use an alternative name instead of the StringBuilder class. It's a small feature but sometimes can be used to make your code easier to read. Our sample code is going to print the following text to the console:

Code is like humor. When you have to explain...
lock icon The rest of the chapter is locked
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 £13.99/month. Cancel anytime
Visually different images