July 2018
Intermediate to advanced
400 pages
12h 14m
English
Occasionally, you may want to use an extension or an imported class whose name is less than ideal in some way. Perhaps it is a difficult-to-remember acronym, or maybe you already have a class with the same name in your file. If you want the function of an imported function or class but not its name, you can use the as operator to assign a different name to be used within the file.
For example, in Tavern.kt you could change the name of the imported random function to randomizer:
Listing 18.16 The as operator (Tavern.kt)
import com.bignerdranch.nyethack.extensions.random as randomizer
...
private fun selectHometown() = File("data/towns.txt")
.readText()
.split("\n")
.random()
.randomizer()
...
And with ...
Read now
Unlock full access