Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Types

Every value in Andy C++ has a type, but you rarely have to write one down. Before the program runs, Andy C++ works out the type of everything in it and reports code that can’t work. Annotations on variables, parameters and return values are optional.

Mistakes found before the program runs

A mistake is found even in code that never runs, and the error points through the calls to the line that can’t work. This program prints nothing:

fn inc(x) => x + 1;
fn shout(s) => inc(s) ++ "!";
print("hello");
if false {
    shout("a");  // error: No function called '+' found that matches the arguments 'String, Int'
}

An anonymous function knows the types of its parameters:

struct Player { name: String, score: Int }
let players = [Player("ann", 3), Player("bob", 5)];
players.map(fn(p) => p.score ++ " points");
// error: No function called '++' found that matches the arguments 'Int, String'

An empty list gets its type from what you put in it:

let xs = [];
xs.push(1);
xs.push("two");  // error: No function called 'push' found that matches the arguments '[Int], String'

If nothing is put in it, annotate it: let xs: [Int] = [];.

The types

Every value is an Any. The other types are:

  • Any
    • Option<T>
    • Bool
    • Int — checked signed 64-bit integers
    • Float — IEEE 754 f64 values
    • Number — arbitrary-size, rational, floating-point, and complex values
    • Sequences, which a for loop iterates over:
    • fn(A, B) -> C: a function taking an A and a B and returning a C
    • Structs: user-defined record types, one per struct declaration

These are also the names you write in annotations. Generic types take their parameters in angle brackets:

let xs: [Int] = [1, 2, 3];
let table: %{String: Int} = %{"a": 1, "b": 2};
let maybe: Option<String> = Some("hi");
let pair: (Int, String) = (1, "hi");
let grid: [[Int]] = [[1, 2], [3, 4]];

print(maybe, pair, grid);  // Some("hi") (1,"hi") [[1,2],[3,4]]

Struct names are type names like any other:

struct Point { x: Int, y: Int }

fn manhattan(p: Point) => p.x + p.y
let points: [Point] = [Point(1, 2), Point(3, 4)];
print(points.map(manhattan));  // [3,7]

Values of different types

The elements of a list, the keys and values of a map, the branches of an if, and the values a function returns all share one type. Numbers mix: [1, 2.5] is a list of Floats. Values that share no type are an error:

let mixed = [1, "a"];  // error: values of different types: Int and String

To mix them, say that they are Anys:

let b: Any = if true { 1 } else { "a" };
let xs: [Any] = [1, "a"];
let m: %{String: Any} = %{"a": 1, "b": "two"};
print(b, xs, m["b"]);  // 1 [1,"a"] two

Write the Any where the list or map is made: a list made as a [String] can’t be used as a [Any] later.

Getting a value out of an Any

Anything can go into an Any, but to get a value back out you need a cast. cast::<Int>(value) checks that the value is an Int, and cast?::<Int>(value) gives an Option<Int>:

let a: Any = 5;
let n: Int = a;  // error: mismatched types: found Any but expected Int
let a: Any = 5;
let n: Int = cast::<Int>(a);
print(n + 1);           // 6
print(cast?::<String>(a));    // None

The same goes for operators and functions: a + 1 needs cast::<Int>(a) + 1. Functions that take any value, like print, string and ==, take an Any as it is.

Indexing is the exception: a[i] works on an Any and gives an Any, so you can read nested data and cast what you find:

let data: %{String: Any} = %{"scores": [3, 5]};
print(cast::<Int>(data["scores"][1]) + 1);  // 6