axiom
Every program has axiom, with no import. It holds printing, strings, turning a value into text, a program's resource
files, and the bytes and utf types.
Printing
| Member | What it does |
|---|---|
print(x) | Prints x as text, with no line break after it: anything string(x) takes |
println(x) | Prints x as text and ends the line |
println() | Ends the line, or prints an empty one |
What a program prints shows in IntelliJ's run window.
print("Ready")
println(", set, go")
println()
println("after an empty line")
Ready, set, go
after an empty line
print and println take any value string(x) takes, so println(5) prints 5 and println(21.5) prints 21.5.
A value that may be missing prints as its value, or as none when there's nothing there:
println(5)
println(21.5)
let comma: i32? = "a,b".find(",")
println(comma)
5
21.5
1
Strings
A string is text. Its length and its index count code points, so an accented letter or an emoji is one.
| Member | What it does |
|---|---|
s.length | How many code points s has |
s[i] | The code point at index i, as a utf, counting from 0 |
s.slice(start, end) | The text from index start up to, but not including, end |
s.contains(t) | Whether t appears in s |
s.startsWith(p), s.endsWith(x) | Whether s begins with p, or ends with x |
s.trim() | s without the white space at either end |
s.find(t) | Where t first appears, as an i32?: nothing when it doesn't |
s.split(separator) | The pieces between each separator, as a string[] |
a + b | a and b joined |
==, !=, <, <=, >, >= | Compare two strings |
let line = " motor A, 50 ".trim()
let first: utf = line[0]
let name = line.slice(0, 7)
let parts = line.split(", ")
println(name + " has " + string(line.length) + " letters")
println(line.startsWith("motor"))
if (let at? = line.find(",")) {
println("comma at ${at}")
}
motor A has 11 letters
true
comma at 7
An index or a slice outside the string ends the run with a fault.
A value as text
| Form | What it does |
|---|---|
string(x) | x as text: an integer, a float, a boolean, a utf, or a class with its own toString(). A float is its shortest decimal, so string(21.5) is "21.5" |
x.toString() | The same, written after the value |
"count: ${n}" | Puts n into the text where the ${…} is |
i64("12345"), f64("1.5"), boolean("true") | A number or a boolean from text written in the program |
let n = 7
println("n is " + string(n))
println("n is " + n.toString())
println("n is ${n}")
println("ready: " + string(true))
println("half of 43 is " + string(21.5))
let big = i64("12345")
println(big + 1)
n is 7
n is 7
n is 7
ready: true
half of 43 is 21.5
12346
A class gives itself a text form with a toString(), and then string(…) and ${…} use it:
class Point(let x: i32, let y: i32) {
let toString(): string = "(${x}, ${y})"
}
The forms that read text, such as i64, take text written in the program, and the build reads it as the program
compiles.
Text and a number don't join with +. The build says to write string(5):
println("count: " + 5)
Resource files
Resources.data(path) gives a program a file it ships, as bytes.
Resource files says where the files go and how a path is written.
let greeting: bytes = Resources.data("greeting.txt")
println(string.fromUtf8(greeting))
With greeting.txt holding Hello, it prints:
Hello
bytes and utf
bytesis raw data, au8[]: whatResources.datagives.data.sizeis how many bytes it holds,data[i]is one of them, andstring.fromUtf8(data)reads them as text.utfis one code point: what a string's index gives.string(c)makes it a one-letter string.
let data: bytes = Resources.data("greeting.txt")
println(data.size)
println(data[0])
With the same greeting.txt, it prints its size, then its first byte, the H:
5
72