Basic types
Every value in Zyn has a type, known before the program runs. Most of the time Zyn works it out by itself:
let count = 42 // Int
let price = 9.99 // Float
let ready = true // Bool
let letter = 'z' // Char
let name = "Zyn" // String
println("{count} {price} {ready} {letter} {name}")42 9.99 true z Zyn
#Numbers
Zyn has two number types:
| Type | What it holds | Examples |
|---|---|---|
Int | A 64-bit whole number | 0, 42, -7, 1_000_000 |
Float | A 64-bit floating point number | 3.14, -0.5, 2.0 |
Underscores in numbers are only for reading: 1_000_000 is a million.
#Arithmetic
+, -, *, / and % (remainder). Dividing two Ints gives an Int, without the fraction. If either side is a Float, the result is a Float:
println(7 / 2)
println(7 % 2)
println(7.0 / 2)
println(1 + 2.5)
println(2 * 3 + 1)
println(2 * (3 + 1))3 1 3.5 3.5 7 8
+=, -=, *=, /= and %= change a var:
var total = 10
total += 5
total *= 2
println(total)30
#No surprises
An Int that grows too big for 64 bits stops the program with an error instead of silently wrapping around, and so does dividing an Int by zero:
let big = 9223372036854775807
println(big + 1)Runtime error: Int overflow: the result is too large --> main.zyn:2:13 | 2 | println(big + 1) | ^
#Converting
Int and Float never turn into each other by themselves. Write 2.0 for a Float, or convert:
let half: Float = 1Type error: Expected a value of type Float, but got Int --> main.zyn:1:19 | 1 | let half: Float = 1 | ^
let n = 7
let f = n.toFloat() / 2
println(f)
println(f.toInt()) // drops the fraction
println(round(3.6))
println(floor(3.6))
println("42".toIntOrNull())3.5 3 4 3 42
Math functions like sqrt, pow, abs, minOf and maxOf are built in, as is PI.
#Booleans
Bool is true or false, with && (and), || (or) and ! (not). && and || only look at their right side when they need to.
let sunny = true
let warm = false
println(sunny && warm)
println(sunny || warm)
println(!warm)false true true
#Comparing
== and != compare values, also of strings, lists and data types. <, <=, > and >= work on numbers, Chars and Strings (alphabetically).
println(3 == 3.0)
println("apple" < "banana")
println([1, 2] == [1, 2])
println('a' < 'b')true true true true
#Characters
A Char is one character, in single quotes. .code gives its Unicode number.
let c = 'Z'
println(c.isLetter())
println(c.lowercase())
println(c.code)
println('7'.isDigit())true z 90 true
#Strings
Strings are text, in double quotes. They have their own page: Strings.
#Any, Unit and Nothing
A few types appear in special places:
Anyis the type of every value (exceptnull; that'sAny?).printlntakesAny?, so it can print anything.Unitis what a function returns when it returns nothing useful, likeprintln. A function without a return type returnsUnit.Nothingis the type of something that never finishes, likepanic(...). It fits anywhere.
fn check(n: Int): Int {
if n < 0 { panic("negative!") } // panic is Nothing, so it fits in an Int function
return n
}
let things = listOf<Any>(1, "two", 3.0, true)
for thing in things {
if thing is String {
println("a string: {thing}")
}
}
println(check(5))a string: two 5
#Type checks and casts
is checks a value's type. After an is check (or a != null check), Zyn knows the narrower type: this is a smart cast.
fn describe(x: Any): String {
if x is Int {
return "an Int, doubled {x * 2}"
}
if x is String {
return "a String of {x.length} characters"
}
return "something else"
}
println(describe(21))
println(describe("hello"))
println(describe(2.5))an Int, doubled 42 a String of 5 characters something else
#Type inference
Types of variables, of lambdas' parameters (where the function they're passed to says what they are) and of expression-bodied functions are inferred. Function parameters always have their types written, and so do the return types of block-bodied functions that return something.
fn area(w: Float, h: Float) = w * h // returns Float, inferred
let sizes = [1.5, 2.0] // List<Float>
let areas = sizes.map { area(it, it) } // List<Float>: `it` is a Float
println(areas)[2.25, 4.0]