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
Modules (not implemented)
module Utilities {
fun appendTest(value: String): String = "${value} test"
}
Utilities.appendTest("string")
"string test"
Namespaces (not implemented)
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 { "not a number" }
... .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
Precedence | Operator |
---|---|
1 | = |
2 | || |
3 | && |
4 | ==!=<<=>>====!== |
5 | +-|^ |
6 | */& |
1 + 1
2
23.0 + 42.0 / 2.0
44.0
Basic Types
Strings
let a = "foo"
"foo"
let c = a + "bar"
"foobar"
c.size()
6
c.isEmpty()
false
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
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
...
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 ...")
HashMap(("hello", "a greeting"), ("goodbye", "a parting"), ("ciao", "not sure ..."))
map.remove("ciao")
"not sure ..."
Array
...