Literals

Numbers

1 -23 123'456 // some integer values of type Int64 1i32 -23i32 123'456i32 // some integer values of type Int32 1.0 -23.0 123'456.0 // some floating-point values of type Float64 1.0f32 -23.0f32 123'456.0f32 // some floating-point values of type Float32

Booleans

true false // the two values of type Bool

Strings

"abc" ""
// some values of type String

Definitions

Constants

let a = 1 // the Int64 value 1 assigned to the constant a a 1 a = 2 // compilation error – a constant cannot be reassigned

Variables

var a = 1 // the Int64 value 1 assigned to the variable a a 1 a = 2 * 3 6

Functions

fun sum(a: Int64, b: Int64): Int64 = a + b
// a function accepting two arguments of type Int64
// and returning a value of type Int64
sum(12, 23) 35

Classes

class Person(let firstName: String, let lastName: String, var age: Int64) { fun fullName: String = "${self.firstName} ${self.lastName}" } let alice = Person("Alice", "Smith", 23) alice.lastName "Smith" alice.fullName "Alice Smith" alice.age = 24 alice.age 24

Unions

union Pet of Cat, Dog // types Cat and Dog defines elsewhere

Static methods

class Utilities { @static fun appendTest(value: String): String = "${value} test" } Utilities.appendTest("string") "string test"

Control Flow

Conditions

let a = 2 if a == 1 { println("a is one") } else { println("a is not one") } a is not one fun describeFloat(float: Float64): String = if float ... === Float64.NaN { "nonumber" } ... .isFinite.not { "infinity" } ... >= 0.0 { "positive" } else { "negative" } describeFloat(123.4) "positive" describeFloat(1.0/0.0) "infinity"

Loops

for i in List[Int64](1, 2, 3) { print(i.toString) } "123"

Operators

From strongest precedence to weakest:
Precedence Operator
1 */&
2 +-|^
3 ==!=<<=>>====!==
4 &&
5 ||

Basic Types

Numbers

1 + 1 2 23.0 + 42.0 / -2.0 -44.0 23.0.toInt64 * 3 69

Booleans

true.not false true && false false true || false true

Strings

let a = "foo" "foo" let c = a + "bar" "foobar" c.size 6 c.isEmpty false c.contains("oba") true c.startsWith("f") true

Options

let some = Some[String]("value") Some("value") let none = None[String]() None some.isSome true none.isSome false some.contains("value") true some.contains("other") false none.contains("value") false

Collection Types

Lists

let list = List[Int64](30, 10, 20, 40) List(30, 10, 20, 40) list.size 4 list(0) // same as list.get(0) 30 list(0) = 5 // same as list.set(0, 5) List(5, 10, 20, 40) list.first Some(5) list.last Some(40) d.removeAt(3) List(5, 10, 20) d.push(40) List(5, 10, 20, 40)

Set

let set = HashSet[String]("hello", "goodbye") HashSet("hello", "goodbye") set.contains("hello") true set("hello") // same as set.get("hello") Some("hello") set.insert("ciao") None set.remove("ciao") Some("ciao")

Maps

let map = HashMap[String, String](("hello", "a greeting"), ("goodbye", "a parting")) HashMap(("hello", "a greeting"), ("goodbye", "a parting")) map.contains("hello") true map.get("hello") Some("a greeting") map.insert("ciao", "not sure ...") None map.remove("ciao") Some("not sure ...")

Array

let array = Array[Int64](30, 10, 20, 40) Array(30, 10, 20, 40) array.size 4 array(0) // same as array.get(0) 30 array(0) = 5 // same as array.set(0, 5) List(5, 10, 20, 40) array.first Some(5) array.last Some(40)