Types
Numbers
Section titled “Numbers”Int and Long hold whole numbers. Double and Float hold fractions. A literal with a . is a Double, one ending in L a Long, one ending in f a Float.
val coins = 250val totalXp = 13_034_431Lval dropChance = 0.05val speed = 1.5fArithmetic
Section titled “Arithmetic”+, -, *, / and % work on numbers. Dividing two whole numbers drops the remainder.
val coins = 250val perPlayer = coins / 3val leftOver = coins % 3val exact = coins / 3.0Converting numbers
Section titled “Converting numbers”A number changes type only when you ask: toInt(), toLong(), toDouble().
val dropChance = 0.05val percent = (dropChance * 100).toInt()val big = percent.toLong()Booleans
Section titled “Booleans”Boolean is true or false. &&, || and ! combine them.
val members = trueval level = 30
val canEnter = members && level >= 25val turnedAway = !canEnterCharacters
Section titled “Characters”Char is one character, in single quotes.
val grade = 'A'val isLetter = grade.isLetter()Strings
Section titled “Strings”String is text, in double quotes. Strings have functions for case, trimming, splitting and searching.
val input = " Bronze Sword "val name = input.trim().lowercase()val words = name.split(" ")val isWeapon = name.endsWith("sword")String templates
Section titled “String templates”$name puts a value in a string. ${} puts in any expression.
val item = "rune scimitar"val price = 25_000
val label = "$item: ${price / 1000}k coins"Multiline strings
Section titled “Multiline strings”Triple quotes hold several lines. trimIndent() removes the shared indent.
val rules = """ No trading in the arena. No running in the bank.""".trimIndent()List keeps items in order. listOf makes a list that can’t change, mutableListOf one that can.
val starter = listOf("bronze sword", "wooden shield")val bank = mutableListOf("bones")
bank.add("coins")bank += starterval first = bank[0]Set holds each item once.
val visited = mutableSetOf("Lumbridge")
visited.add("Varrock")visited.add("Lumbridge")val count = visited.sizeMap looks up a value by its key. to pairs a key with a value.
val prices = mutableMapOf("bones" to 1, "feather" to 2)
prices["arrow"] = 5val price = prices["feather"] ?: 0Working with collections
Section titled “Working with collections”filter, map, sumOf, sortedBy and friends make new collections from old ones.
class Item(val name: String, val price: Int)
val shop = listOf(Item("bread", 4), Item("cake", 50), Item("pie", 20))
val cheap = shop.filter { it.price < 30 }.map { it.name }val total = shop.sumOf { it.price }val dearest = shop.maxBy { it.price }Type checks
Section titled “Type checks”is checks a type. After the check, the value is that type.
fun describe(thing: Any): String = if (thing is String) "text of length ${thing.length}" else "something else"as treats a value as another type. as? gives null instead of failing.
val thing: Any = "coins"
val text = thing as Stringval number = thing as? IntAny, Unit and Nothing
Section titled “Any, Unit and Nothing”Any is every type. Unit is the result of a function that gives nothing back. Nothing is the result of one that never returns.
fun log(message: Any): Unit { Logger.info(message.toString())}
fun refuse(reason: String): Nothing = throw IllegalStateException(reason)Type aliases
Section titled “Type aliases”typealias gives a type another name.
typealias Price = Int
fun discounted(price: Price): Price = price * 9 / 10