liveBook · Manning

Scope

As explained in F# in Action: A Revised Edition of Get Programming with F#

A scope is a region of source code that determines the lifetime and accessibility of names and bindings.

Scope is the region of code in which a symbol can be referenced. It controls a binding’s lifetime and accessibility, helping code state which names belong together and where they are intended to be used. section 3.1.3 section 3.4

F# expresses scope with whitespace indentation rather than curly braces. Lines indented to the same level belong to the same body; increasing indentation creates a nested scope. Four spaces per level is a common convention, and tabs are not valid F# tokens. section 3.1.3

let greetingText =                #1
    let fullName =                #2
        let fname = "Frank"       #3
        let sname = "Schmidt"
        $"{fname} {sname}"        #4
    $"Greetings, {fullName}"      #5

Nested let bindings build a full name inside the scope that produces a greeting. The inner names support the result without needing to become top-level bindings. section 3.1.3

Use nested scopes to keep intermediate work close to the value that needs it:

  • Bind temporary values with let, then return the final expression from the indented block. section 3.1.3
  • Move a nested calculation into a separate function when it should be reused or tested independently. section 4.1.5
  • After extracting a scope, pass required outer symbols as function inputs and let the compiler infer types where possible. section 4.1.5

Extracting a reusable calculation

A function that both calculates an age description and prints it ties the calculation to console output. Extract the age-description logic into a function, call it from the printing function, and the calculation can serve other outputs and unit tests. section 4.1.5

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains indentation and nested bindings, then gives a procedural exercise for extracting a nested scope into a function.
Examples
Uses a nested full-name greeting and an age-description calculation separated from console output.
What it assumes
Builds from prior function definitions while introducing whitespace-based scope through short binding examples.
What you can do after
Practice/perform it by nesting bindings, diagnosing indentation-related errors, and extracting reusable calculations into functions.
How it connects
Connects scope with let bindings, immutability, type inference, functions, refactoring, and unit testing.

Also explained in

Compare how 21 books explain scope →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.