_ is the wildcard pattern that matches any value without introducing a variable binding.
Syntax
;; In patterns (`match`, `define`, `fn`, `let`, `do`, `catch`, `handle`)
_
;; Explicitly discard an expression's result(s) in a body
(let _ = ⟨expr⟩)
(let (values _ ...) = ⟨expr⟩)
;; Ignored binder in a comprehension clause or short lambda
[... :for _ :below ⟨ufix⟩ ...]
[... :for _ :in ⟨iter⟩ ...]
[... :with _ = ⟨expr⟩ ...]
ƒ_.⟨expr⟩
Semantics
_matches any value of the expected type and discards it without binding a variable.- Multiple
_wildcards may appear in the same pattern, parameter list,valuesbinding, orƒparameter list without conflicting. _is not a valid expression or variable name and cannot be evaluated.- Discarding results with
(let _ = ⟨expr⟩):- Unlike a named pattern variable,
(let _ = ⟨expr⟩)accepts expressions returningVoid(zero values) or multiple values as well as single values. - Using
(let _ = ⟨expr⟩)suppresses compiler warnings when discarding aResultvalue, which would otherwise trigger a discarded-result warning.
- Unlike a named pattern variable,
- In
catchandhandle:- A branch
(_ ⟨body⟩+)catches any Common Lispcl:error(including all Coalton exceptions and panics). - A branch
((the ⟨exception-type⟩ _) ⟨body⟩+)catches any exception of⟨exception-type⟩without binding it.
- A branch
Examples
(coalton-toplevel
;; Ignore unused tuple fields and constructor payloads
(declare first-or-zero ((Optional (Tuple Integer String)) -> Integer))
(define (first-or-zero opt)
(match opt
((Some (Tuple x _)) x)
((None) 0)))
;; Discard the second return value of a multi-value call
(declare quotient-only (Integer * Integer -> Integer))
(define (quotient-only a b)
(let (values q _) = (values (coalton/math:div a b) (mod a b)))
q))