Core
/ tutorial

Language

Keywords

Reserved keywords are regular, contextual keywords are italic.
To-be-added keywords have a green underline, to-be-removed keywords are struckthrough.

Namespacing

import
bring existing type into scope examples:  import core.String ,   import core.collection.{ List, Set, Map }
mod (to be removed)

Type Definitions

class
reference type examples:  class String ,   class List[T] ,   class Map[K, V]
value
value type examples:  value Bool ,   value Int64 ,   value Float32
union
union type ( of  lists variants of the union type) examples:  union Pet of Cat, Dog, Fish
trait
interface type ( for  defines a trait implementation) examples:  trait Hash ,   trait Hash for String
enum enum type (to be replaced by union)
impl trait implementation (to be removed)

Member Definitions

fun function
let value
var variable
const constant value (to be replaced by let)

Control Flow

if else unified condition expression ( is used for pattern matching)
return return specified value from current function
while looping construct
for in looping construct (to be replaced by streaming combinators)

Qualifiers

Self type qualifier, refer to current type
self value qualifier, refer to current instance

Values

true false literals of the two boolean values

Annotations

annotation
annotation definition examples:  annotation static ,   annotation pub ,   annotation test

Operators

From strongest precedence to weakest.

Unary

Precedence Operator
0 +-

Binary

Precedence Operator
1 */&
2 +-|^
3 ==!=<<=>>====!==
4 &&
5 ||

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

while loop
var i = 1 while i <= 3 { print(i.toString) i = i + 1 } "123"
for loop
for i in List[Int64](1, 2, 3) { print(i.toString) } "123"

Standard Library

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

Option & Result Types

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

Results

let pass = Pass[String, Unit]("value") Pass("value") let none = Fail[Unit, String]("error") Fail("error") pass.isPass true fail.isPass false pass.contains("value") true some.contains("other") false fail.contains("error") true pass.or(none) Pass("value")

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)

Sets

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 ...")

Arrays

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)

Module System

(Not implemented yet.)

A module.core file defines the module's metadata, including name, version, dependencies and exported packages:

module com.example.lib {
  version      = "1.2.34"
  exports      = foo.bar, foo.baz, foo.qux
  dependencies = com.example.core, com.example.std
  licenses     = [ "MPL2" ]
  source       = git:code.example.com
  website      = example.com/lib
  authors      = Eve <eve@example.com>, Joe <eve@example.com>
}

The module's name is a reverse domain name (possibly including a sub-domain component).
Control of the domain is validated through DNS TXT records, RFC5785 or other sufficient means.

The location of the module.core file defines the module root directory.

A source file's package path is the combination of the module name and directories underneath the module root; therefore a source file of the module com.example.lib placed in foo/bar/ is inside the package com.example.lib.foo.bar