# Literals and ranges

> Write numeric, string, symbol, array, hash, regular expression, and heredoc literals.

Canonical: https://alice-lang.org/documentation/language/literals/

[Documentation index](https://alice-lang.org/documentation/llms.txt) · [Complete book](https://alice-lang.org/llms-full.txt)

## Literal forms

| Form            | Meaning                                      |
| --------------- | -------------------------------------------- |
| `42`            | `Int32` integer                              |
| `42L`           | `Int64` integer                              |
| `2.5`           | `Float64` number                             |
| `2.5f32`        | `Float32` number                             |
| `true`, `false` | Boolean values                               |
| `'x'`           | Character                                    |
| `"tea"`         | String                                       |
| `:tea`          | Symbol-like name; prints as `tea`            |
| `nil`           | No value, within a nullable type             |
| `[1, 2, 3]`     | Fixed-size .NET array, inferred element type |
| `{"tea" => 2}`  | Hash/dictionary                              |
| `%w[tea cake]`  | Array of words                               |
| `%i[tea cake]`  | Array of symbol-like names                   |

Use `_` to separate digits in numeric literals, such as `1_000_000`. Hexadecimal and binary forms use `0x` and `0b`. For explicit numeric storage types, see [types](https://alice-lang.org/documentation/language/types.md).

## Strings and interpolation

Double-quoted strings interpolate expressions inside `#{...}`. Escape sequences such as `\n` and `\t` represent newlines and tabs.

```alice
name = "Alice"
puts "Hello, #{name}!\nWelcome."
```

The [string library](https://alice-lang.org/documentation/library/strings.md) covers transformation, searching, and formatting. Single quotes denote characters; use double quotes for strings.

## Arrays and hashes

An array literal infers its element type. For an empty typed array, use `Array(Int32).new(0)`. A growable list is `List(Int32).new`; use `.Add` to append.

A hash maps keys to values. `{"a" => 1}` infers `Hash(String, Int32)`, which is a .NET dictionary. For an empty hash, use `Hash(String, Int32).new`.

## Inclusive and exclusive ranges

```alice
puts (1..5).sum   # 15; includes 5
puts (1...5).sum  # 10; stops before 5
```

Ranges are useful in `for`, iterator blocks, and numeric operations. `0...xs.size` describes valid indices for an array. A range does not need to allocate an array just to iterate. Use `.to_a` when you need a stored collection.

## Regex literals

`/\d+/` creates a regular expression. Flags `i`, `m`, `x`, and `s` select .NET regex options. Literal regex instances are cached rather than rebuilt on each match.

```alice
puts "Alice42".match?(/\d+/) # true
```

An arithmetic expression such as `a/2` is division. Read [regex operations](https://alice-lang.org/documentation/library/regex.md) for match results and substitution.

## Heredocs

Use a heredoc for multiline text. `<<~NAME` strips common indentation and supports interpolation; `<<-NAME` keeps the content's indentation and allows an indented terminator.

```alice
message = <<~TEXT
  Hello, Alice.
  Follow the rabbit.
TEXT
puts message
```

Choose a terminator that does not occur as a content line. The terminator closes the literal; it is not printed as part of the string.

## Continue reading

- Previous: [Variables and assignment](https://alice-lang.org/documentation/language/variables.md)
- Next: [Types and nil safety](https://alice-lang.org/documentation/language/types.md)
