liveBook · Manning

Wildcard

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

A wildcard is a pattern or placeholder that matches a value without binding or using its contents.

In F#, the underscore (_) is a wildcard: it matches a value while deliberately leaving its contents unused. It keeps bindings honest when a tuple contains data you do not need, rather than giving that data a misleading temporary name. section 5.2.2

Extracting only the needed tuple field

Given let nameAndAge = "Jane", "Smith", 25, the binding let forename, surname, _ = nameAndAge takes the first two values and discards the age. The underscore signals both to the type system and to readers that the third value is intentionally unused. section 5.2.2

let nameAndAge = "Jane", "Smith", 25     #1
let forename, surname, _ = nameAndAge    #2

Inspect a short tuple deconstruction where _ drops the final element without inventing a name for it. section 5.2.2

A wildcard can also be the final branch of a pattern match, standing for every value not handled by earlier patterns. Match cases run in order, so placing _ first prevents later cases from being reached. It can make a rule set succinct, such as accepting every loan applicant except the explicitly rejected cases. section 8.1.3 section 8.1.1

Use a wildcard with care in pattern matching:

  • Put it last when it is a fallback; otherwise it swallows all later cases. section 8.1.1
  • Handle unexpected enum values explicitly when useful, for example by throwing a specific exception or returning an error value. section 8.1.1
  • Prefer explicit cases for discriminated unions that may grow. A wildcard can hide a newly added case from compiler warnings. section 8.3.4 section 14.1.1
  • Model bounded domain states with discriminated unions when possible, so exhaustive matching identifies exactly what is missing. section 8.3.4

Wildcards are useful for intentional omission and compact rules, but they trade away some compiler assistance. With a discriminated union, explicit alternatives let the compiler flag every match affected by a new case; a broad _ branch silently routes that case to existing behavior. Treat the shortcut as deliberate, not automatic. section 8.3.4 section 14.1.1

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

How deep it goes
Explains tuple discards, ordered fallback matching, compiler warnings, and maintenance consequences for discriminated unions.
Examples
Uses name-and-age tuple deconstruction, loan-decision rules, SMS contact methods, and instrument matching.
What it assumes
Assumes basic F# bindings and introduces wildcard use through tuples and pattern matches.
What you can do after
Apply it to discard unneeded values and choose explicit or fallback pattern-match cases deliberately.
How it connects
Connects wildcards to tuples, pattern matching, type inference, discriminated unions, enums, and compiler warnings.

Also explained in

Compare how 8 books explain wildcard →

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.