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...
- Import the
StringBuilderclass with a custom alias:
import java.lang.StringBuilder as builder
- Use the custom
StringBuildername 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...