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

Search

Menu

Indentation and Scope

Halyard uses significant indentation to define code structure, similar to Python or Haskell. Understanding how indentation works is essential for writing correct Halyard code.

The Core Rule

Within a block, lines at the same indentation level are separate statements. Indentation is also used for function bodies, case branches, and other constructs that require nested content.

For function arguments, the rule is more specific: collections (lists, records, tuples, parenthesized expressions) on an indented new line continue as arguments to the previous function call.

Where a Statement Ends

A line break ends the statement, unless the next line continues it. A line break is a continuation in exactly three cases:

  1. The next line is more deeply indented — those lines belong to the construct above them.
  2. The next line begins with an operator (|>, +, ,, ::, ->, ., …), whatever its indentation.
  3. The current line ends open — its last token is an operator or an opening (, [ or {.

Anything else is two statements, and nothing can reach across the break between them:

halyard
-- A line break ends the statement unless the next line continues it
import List

let names = ["Ada", "Grace", "Alan"]

-- `names` is the whole argument: the list below is its own statement,
-- not a second argument to List.length
let n = List.length names
[1, 2, 3]

n

List.length names is finished at the end of its line, so the list below is a statement of its own rather than a second argument. This is what makes the rest of this page predictable: once you know where a statement ends, indentation only has to tell you what belongs inside it.

Basic Blocks

Function bodies must be indented from the function definition:

halyard
-- Function body must be indented
fn addOne : Int -> Int
fn addOne x =
    x + 1

addOne 5  -- 6

Multiple statements at the same indentation form a block:

halyard
-- Multiple let bindings at same indentation form a block
fn compute : Int -> Int
fn compute x =
    let a = x + 1
    let b = a * 2
    let c = b - 3
    c

compute 5  -- 9

Multiline Function Arguments

Function arguments can be placed on new indented lines, but only for collections (lists, records, tuples) and parenthesized expressions.

Collections as Arguments

Lists, records, and tuples on an indented new line continue as arguments:

halyard
-- Multiline arguments work with COLLECTIONS (lists, records, tuples)

-- NOT with simple values like integers

import List

-- This works: list on new indented line continues as argument
let result = List.map (|x| x * 2) [1, 2, 3]

result  -- [2, 4, 6]

The list [1, 2, 3] is indented from List.map (|x| x * 2), so it's parsed as the next argument.

Elm-style Multiline Lists

For readability, you can use leading commas with lists on multiple lines:

halyard
-- Multi-line function calls work with collections on new lines
import List

-- Lambda on same line, list on indented new line
let doubled = List.map (|x| x * 2) [1, 2, 3]

-- Equivalent to: List.map (|x| x * 2) [1, 2, 3]
doubled  -- [2, 4, 6]

This style (common in Elm) keeps each element aligned and makes diffs cleaner.

Separate Statements

Lines at the same indentation level are separate expressions, evaluated in sequence:

halyard
-- Lines at the SAME indentation are separate expressions
let x = 1

let y = 2

let z = 3

x + y + z

Each let binding starts at the same column, making them separate statements in the block.

Multiline Arguments: Module Functions

Multiline argument continuation works with module functions (like List.map, String.length). You can place collections or parenthesized expressions on indented new lines:

halyard
import List

List.map (|x| x * 2) [1, 2, 3]

Local Functions: Same-line Arguments

For local functions defined with fn or let, arguments should stay on the same line:

halyard
fn add : Int -> Int -> Int
fn add x y =
    x + y

let result = add 1 2

result

Indented lines become separate expressions, not arguments:

halyard
-- NOT correct: indented line becomes separate expression
let result = add 1
    2           -- This is a separate expression, not an argument!

Pipes and Method Chains

Pipe operators work naturally with multiline formatting. Each pipe continues the expression:

halyard
-- Pipes work naturally with multiline formatting
import List

let result =
    [1, 2, 3, 4, 5]
        |> List.map (|x| x * 2)
        |> List.filter (|x| x > 4)
        |> List.foldl (|acc x| acc + x) 0

result  -- 6 + 8 + 10 = 24

The pipes are indented from the initial list, so they continue as part of the same expression chain.

The extra indentation is a convention, not a requirement. A |> that opens a line continues the line above it by case 2 of Where a Statement Ends, so the same-column form means exactly the same thing:

halyard
-- An operator-led line continues the line above it at any column
import List

let result =
    [1, 2, 3, 4, 5]
    |> List.map (|x| x * 2)
    |> List.filter (|x| x > 4)
    |> List.foldl (|acc x| acc + x) 0

result

What the pipes do have to be is consistent with each other: every continuation line in one chain must sit at the same column.

Case Expressions

Case branches must be indented from the case keyword:

halyard
-- Case branches must be indented from 'case'
let value = 2

let result =
    case value of
        1 -> "one"
        2 -> "two"
        _ -> "other"

result  -- "two"

Branch bodies can span multiple lines with further indentation:

halyard
-- Case branch bodies can span multiple lines
let value = 2

let result =
    case value of
        1 ->
            let x = 10
            x + 1
        2 ->
            let x = 20
            let y = 5
            x + y
        _ -> 0

result  -- 25

Each branch body is indented from the ->, and statements within the body are at the same level.

If-Then-Else

Multi-line if expressions work similarly:

halyard
-- If-then-else with multiline expressions
let condition = True

let result =
    if condition then
        1 + 2
    else
        10 * 2

result

The then and else branches are indented, and multiple statements within each branch are at the same indentation level.

Scoping

Indentation creates nested scopes. Inner scopes can access variables from outer scopes:

halyard
-- Indentation creates nested scopes within a block
fn example : Int -> Int
fn example x =
    let a = x + 1
    let b = a * 2
    let c = a + b
    c

example 5  -- a=6, b=12, c=18 → 18

Variables are visible in all nested scopes (blocks indented further), but not in sibling or parent scopes.

Scope Rules

  1. Sequential visibility: Within a block, later statements can see earlier bindings
  2. Not inner to outer: Variables defined in inner scopes are not visible outside
halyard
fn example : Int -> Int
fn example n =
    let a = n + 1
    let b = a * 2
    let c = a + b
    c

example 2

Variables a, b, and c are not visible outside the function body.

Lists and Records

Collection literals can span multiple lines using Elm-style leading comma syntax:

halyard
-- Lists can span multiple lines (Elm-style)
import List

let numbers = [1, 2, 3, 4, 5]

List.sum numbers

-- 15
halyard
-- Records can span multiple lines (Elm-style)
let person =
    { name = "Alice"
    , age = 30
    , city = "Paris"
    }

person.name

The leading comma style aligns elements neatly and makes git diffs cleaner (adding an element only changes one line).

Common Mistakes

Mistake 1: Accidental Continuation

halyard
-- WRONG: 'print' is indented, so it's parsed as an argument to 'compute x'
let result = compute x
    print result

-- CORRECT: 'print' at same level as 'let'
let result = compute x
print result

Mistake 2: Missing Indentation

halyard
-- WRONG: body not indented
fn example : Int -> Int
fn example x =
x + 1            -- Error: expected indented block

-- CORRECT: body indented
fn example : Int -> Int
fn example x =
    x + 1

Mistake 3: Inconsistent Indentation

halyard
-- WRONG: mixed indentation in block
fn example : Int -> Int
fn example x =
    let a = 1
      let b = 2    -- Error: inconsistent indentation
    a + b

-- CORRECT: consistent indentation
fn example : Int -> Int
fn example x =
    let a = 1
    let b = 2
    a + b

Best Practices

  1. Use 4 spaces per indentation level (recommended)
  2. Be consistent — don't mix tabs and spaces
  3. Align related code — put sibling statements at the same level
  4. Indent continuations clearly — make it obvious when a line is an argument
  5. Use empty lines to separate logical sections, but maintain indentation

Summary

IndentationMeaning
Further indentedContinues previous expression (as argument)
Same levelSeparate statement in block
Less indentedEnds current block, returns to outer scope

Understanding this rule unlocks the rest of Halyard's syntax. When in doubt, remember: indented = continuation, same level = separate.

Next Steps

Continue to learn about functions in detail.