# Variables and assignment

> Understand inferred local variables, immutable bindings, scope, and assignment operators.

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

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

## Declare a local

An assignment introduces a local variable and infers its type from the initializer. Use `name : Type = value` when a particular type matters.

```alice
val price = 12
quantity = 2
quantity += 1
puts price * quantity
puts 7 / 2
puts 7.0 / 2.0
```

Expected output:

```text
36
3
3.5
```

[Edit and run in the Alice playground](https://playground.alice-lang.org/#code=u.dmFsIHByaWNlID0gMTIKcXVhbnRpdHkgPSAyCnF1YW50aXR5ICs9IDEKcHV0cyBwcmljZSAqIHF1YW50aXR5CnB1dHMgNyAvIDIKcHV0cyA3LjAgLyAyLjA)

`quantity` is mutable, so `quantity += 1` changes its value. `price` uses `val`; assigning another value to it is a compile error (AL0203).

```alice
count : Int64 = 0
name : String? = nil
```

A bare `name = nil` has no underlying type to infer and is rejected with AL0204. State the nullable type explicitly.

## Binding is not deep immutability

`val` prevents replacing the local binding. It does not make an array, list, or object recursively immutable. A mutable object can still change through its methods. For shared work, use the separate [race-safety rules](https://alice-lang.org/documentation/concurrency/race-safety.md).

## Scope and definite assignment

Local variables are function-scoped in the Ruby style. A local introduced in an `if` or loop can be visible after the block, but it must be assigned on every path before it is read.

```alice
if true
  message = "ready"
else
  message = "waiting"
end
puts message
```

A branch that does not assign `message` may produce AL0309. Initialize the variable before the branch, or make the `if` an expression whose branches all return the required type.

## Instance and class state

| Name         | Meaning                                    |
| ------------ | ------------------------------------------ |
| `name`       | Local variable or member access in context |
| `@name`      | Instance field                             |
| `@@name`     | Class/static field                         |
| `Type::NAME` | Qualified constant                         |

Constructor shorthand such as `def initialize(@name : String)` initializes an instance field from a parameter. Properties expose controlled access to that state; see [classes](https://alice-lang.org/documentation/language/classes.md).

## Compound assignment

`+=`, `-=`, `*=`, and `/=` combine an operation and assignment. Their operand types still need a valid operator. `||=` is useful for initialization in supported Ruby-style forms, including class registries. It is not a synchronization operation: two concurrent writers still need a safe communication or locking mechanism.

## Continue reading

- Previous: [Programs, files, and names](https://alice-lang.org/documentation/language/programs.md)
- Next: [Literals and ranges](https://alice-lang.org/documentation/language/literals.md)
