liveBook · Manning

Lightweight syntax

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

Lightweight syntax is a language syntax style that expresses common programming constructs with relatively little punctuation or boilerplate.

F# uses relatively little ceremony to express common programming tasks. Functions, values, and types can focus on the problem being modeled without routinely requiring type annotations or full classes. Compact syntax works alongside a type-safe compiler; it does not mean giving up compiler checks. section 1.1

A function with little ceremony

let addTenThenDouble theNumber = let addedTen = theNumber + 10 let answer = addedTen * 2 answer The final expression supplies the result. A value uses the same `let` pattern: `let mySimpleValue = 99`. section 3.1.1

Listing 3.2 A verbose version of F# syntax basics
let addTenThenDouble (theNumber : int) : int =                          #1
{                                                                       #2
    let addedTen : int = theNumber + 10;                                #3
    let answer : int = addedTen * 2;
    printfn $"({theNumber} + 10) * 2 is {answer}";
    let website : System.Uri = new System.Uri ("https://fsharp.org");   #4
    return answer;                                                      #5
}

Compare the deliberately verbose version with the compact function. It makes visible the annotations, braces, semicolons, and explicit return that the compact version does not need. section 3.1.1

Small defaults also reduce repetition: declarations are public by default, and `not` provides a readable alternative to a negation symbol. Less boilerplate leaves more room for domain logic. Compiler checks still matter: adding a field to an F# type requires its value to be supplied wherever that type is initialized. section 3.1.1 section 2.1.4

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

How deep it goes
Contrasts compact and deliberately verbose versions of a function and explains defaults, but does not detail parsing rules.
Examples
Contrasts two arithmetic-and-printing functions and shows that functions and simple values share a declaration pattern.
What it assumes
Introduces F# declarations with small examples while drawing on familiarity with functions, values, and access modifiers.
What you can do after
Practice/perform compact F# declarations while recognizing which annotations and punctuation the shown examples can omit.
How it connects
Connects let bindings, public-by-default access, domain-specific languages, and compiler checks to concise F# code.

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.