← Back to the index

3. Types and Literals

In this chapter

Booleans

The boolean type has two possible values: true and false.

Numbers

vv supports two types of numbers: 64-bit integers (int) and 64-bit floating-point numbers (float).

let int_num = 42
let float_num = 3.14159

Arithmetic operations between integers result in an integer, while operations involving at least one floating-point number result in a float. Note that the division operator (/) always results in a float. For floor division (integer division), use the /: operator.

Characters and Strings

A single character is represented using single quotes.

let c = 'A'

Strings are represented using double quotes. Strings are entirely treated as a list of characters. String interpolation is supported using the {} syntax.

let name = "World"
let greeting = "Hello, {name}!"

Lists

Lists are ordered collections of elements. They are defined using square brackets []. A trailing comma is allowed after the last element.

let numbers = [1, 2, 3, 4]
let letters = [
    "a",
    "b",
    "c",
]
let empty_list = []

Indexing

Elements in a list can be accessed using 0-based indexing. Note that indices must be of type int.

let numbers = [1, 2, 3, 4]
let first = numbers[0] // 1

Item Assignment

You can overwrite an element at a specific index using the assignment operator =.

let numbers = [1, 2, 3]
numbers[1] = 99
// numbers is now [1, 99, 3]

Slicing

A sub-list can be extracted using the slice syntax list[start:end]. Both start and end indices are optional. If the start index is omitted (e.g., [:end]), slicing begins from the first element (index 0). If the end index is omitted (e.g., [start:]), slicing continues to the end of the list.

let sub = numbers[1:3] // [2, 3]
let first_two = numbers[:2] // [1, 2]
let from_second = numbers[1:] // [2, 3, 4]

Length

The number of elements in a list can be obtained using the len() syntax.

let count = len(numbers) // 4

Records

Records are collections of key-value pairs (also known as dictionaries or objects). They are defined using curly braces {} with key = value pairs. A trailing comma is allowed after the last pair.

let person = {
    name = "Alice",
    age = 30,
}

Field Access

Values in a record can be accessed using dot notation.

let person_name = person.name // "Alice"

Field Assignment

A field in a record can be updated using dot notation on the left-hand side of an assignment.

let person = { name = "Alice", age = 30 }
person.age = 31
// person.age is now 31

Destructuring

Destructuring is a convenient way to extract multiple fields from a record into local variables in a single declaration. This allows you to assign field values to variables with the same name as the record's keys.

let person = { name = "Alice", age = 30 }
let { name, age } = person

// You can now use `name` and `age` directly
console.print(name) // "Alice"
console.print(age)  // 30

If you want to use a different name for a variable, you can use the as keyword to alias a field to a different name. This is useful for avoiding name conflicts or providing clearer variable names.

let { name as person_name, age as person_age } = person

console.print(person_name) // "Alice"
console.print(person_age)  // 30