Skip to content
Halyard
Project
Source code on Codeberg (opens in new tab)

Search

Menu

Variables

Variables in Halyard are bindings that associate names with values. By default, all bindings are immutable.

Declaring Variables

Use let to declare a variable:

halyard
let name = "Alice"

let age = 30

let pi = 3.14159

pi

Type Annotations

Halyard has strong type inference, but you can add explicit type annotations:

halyard
let count : Int = 42

let message : String = "Hello"

let ratio : Float = 0.75

ratio

Type annotations are optional but can improve code clarity and catch errors earlier.

Variable Shadowing

To "update" a variable, use variable shadowing by re-declaring it with the same name:

halyard
let x = 10

let x = x + 5

x  -- 15

Variables itself are immutable. Shadowing creates a new variable that hides the previous one.

halyard
let x = 1

let x = x + 1

let x = "hello"  -- x can change type when shadowed

x  -- "hello"

Note: Only variables support shadowing. Functions, modules, and type definitions do not allow shadowing — redeclaring them with the same name is an error.

Scope

Variables are block-scoped. A variable is only accessible within the block where it's defined:

halyard
fn example : Int -> Int
fn example x =
    let inner = x + 1
    inner

inner  -- Error: `inner` is not in scope

Let Expressions

In Halyard, let is an expression that introduces bindings:

halyard
let area =
    let width = 10
    let height = 5
    width * height

area  -- 50

The value of a let block is the value of its final expression.

Multiple Bindings

You can chain multiple let bindings:

halyard
let x = 1

let y = 2

let z = 3

x + y + z

Destructuring

Variables can be bound using pattern matching (destructuring):

Tuple Destructuring

halyard
let (x, y) = (10, 20)

x  -- 10

Record Destructuring

halyard
let { name, age } = { name = "Bob", age = 25 }

name  -- "Bob"

Nested Destructuring

halyard
let (x, { a, b }) = (1, { a = 2, b = 3 })

a  -- 2

Recursive Let Bindings

A lambda bound with let can call itself by name — the binding is in scope for its own body:

halyard
-- Recursive lambdas bound with let
let fac = |n: Int| if n == 0 then 1 else n * fac (n - 1)

fac 5

This is useful for local helper functions that don't need to be visible outside a block.

Polymorphic Let Bindings

When a let-bound lambda has no type annotation, it is automatically generalized to work with any type. This is called let-polymorphism:

halyard
-- A let-bound lambda can be used at multiple types (let-polymorphism)
let identity = |x| x

let n = identity 1
let s = identity "hello"

n

The lambda |x| x is inferred as ∀a. a -> a, so it can be applied to both Int and String in the same program.

Best Practices

  1. Use descriptive names: userName is better than u
  2. Use shadowing for transforms: Chain let x = ... to transform values step by step
  3. Keep scopes small: Define variables close to where they're used
  4. Use type annotations for public APIs and complex expressions
  5. Use destructuring to extract values from tuples and records

Next Steps

Learn about operators to combine values.