Skip to content

Language tour

1 min read

Learn the basics through four short programs. Run each one before moving on, then change a value or add a statement. The examples introduce one idea at a time.

Variables

A variable is a named box that holds a value. let can be changed later. const cannot.

import system;

fun main() { // (1)
    let name = "Ada"; // (2)
    const n = 3; // (3)
    name = "Grace"; // (4)
    System.println(name); // (5)
    System.println(n);
}
  1. Every program starts in main.
  2. let — Dream infers name is a string from "Ada".
  3. const — n is locked after this line.
  4. Reassign a let with =.
  5. Print a line to the terminal (System.println after import system;).

You can write the type yourself when you want: let score: int = 10;.

See Variables.

Control flow

Control flow chooses which lines run, and how often.

import system;

fun main() {
    let score = 85;

    if score >= 90 { // (1)
        System.println("A");
    } else if score >= 70 {
        System.println("B");
    } else {
        System.println("C");
    }

    let i = 0;
    while i < 3 { // (2)
        System.println(i);
        i = i + 1; // (3)
    }
}
  1. Parentheses around the condition are optional. The condition must be a bool (true or false).
  2. while repeats the block as long as the condition is true.
  3. Change i or the loop never ends.

for (let i = 0; i < 3; i = i + 1) is another way to count. for (let x in list) walks a collection.

See Control flow.

Functions

A function is a named recipe. You pass values in; it can return a value out.

import system;

fun greet(name: string): string { // (1)
    return "Hello, " + name; // (2)
}

fun main() {
    let msg = greet("world"); // (3)
    System.println(msg);
}
  1. fun, then the name, then name: type parameters, then : ReturnType.
  2. + on strings glues them together.
  3. Call greet with "world"; msg is "Hello, world".

fun main() has no return type — it returns nothing.

See Functions.

Lists

A list grows as you add items. Import system.collections for List.

import system;
import system.collections; // (1)

fun main() {
    let xs = List<int>(); // (2)
    xs.push(10); // (3)
    xs.push(20);

    for (let n in xs) { // (4)
        System.println(n);
    }
}
  1. List, Map, and Set live in this package.
  2. List<int>() — an empty list that only holds integers.
  3. push adds at the end.
  4. for..in sets n to each element.

See Arrays and Collections.

What next?

Next steps — reference, cookbook, and community.