Imagine you’re writing universal domain logic for your program. All I/O is provided via an interface (like UI, persistent storage, networking, etc.), you don’t have to think about it, and you almost don’t care about the platform, on which you run. Your program is fairly simple, only a few straightforward tasks. This chapter describes all essentials you need to write it. The foundational basics that you can build upon: data, processing primitives, and functions to reuse code.
Data
At the core of any program, lies the data. The sole purpose is to often receive and transform, sometimes create, but always output the data. A program that doesn’t output anything is useless. Take away variables (or functions that act as variables) and there’s nothing to process. In this section, cornerstone data representations are discussed: primitive data types, variables, collections, ranges, text, optionals, and function types.
Note: Custom data types (structs, classes) are showcased later, in the Abstractions chapter. They help a lot and make your program much easier to reason about, but you don’t absolutely need them. You can get away by structuring your data using collections, Map a.k.a. Dictionary a.k.a. Associative Array in particular. This has been demonstrated by Lua scripting language, for example.
String interpolation
Simple concatenation:
val count = 4
val concatenated = "Count to " + count
String interpolation via templates:
val simpleTemplate = "Count to $count"
val curlyTemplate = "Count to ${count + 1}"
Multiline strings and escaping '$' sign:
var withDollarSign = "Costs \$_5"
withDollarSign = """
Costs ${'$'}_5
"""
Multiline text without indentation
Special methods can remove the initial indentation:
val withoutInitialSpace = """
Nice in code, but ugly when printed.
Can we remove the space, please?
""".trimIndent()
/*---
Nice in code, but ugly when printed.
Can we remove the space, please?
---*/
val withoutDesignatedIndentation = """
|Nice in code, but ugly when printed.
|Can we remove the space, please?
""".trimMargin()
/*---
Nice in code, but ugly when printed.
Can we remove the space, please?
---*/