‥
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
The existing store-policy spellings are the whole-width range: saturating<u8> is saturating<u8, 0 … 255>, u8 is trapping<u8, 0 … 255>.
Syntax: saturating<u8, 0 … 255>
Category#
range
Related#
Examples#
radix/corpus/reference/operator-group/range/range-glyphs.fab (canonical · operator-group)#
Half-open and inclusive range endpoints in for range and ∈.
# =============================================================================
# ‥ — Half-open and inclusive range endpoints in for range and ∈.
# =============================================================================
#
# What this teaches:
# • Exclusive ranges — `‥` (two dots) creates half-open ranges: start inclusive, end exclusive.
# • Inclusive ranges — `…` (three dots) creates inclusive ranges: both endpoints included.
#
# Common mistakes:
# • Confusing ‥ (exclusive range) with … (inclusive range) — off-by-one errors are the most common range bug.
#
# See also: ‥, …, before, until, ∈
# =============================================================================
# operatores/range-glyphs — ‥ (exclusive) and … (inclusive)
#
# GRAMMAR:
# rangeExpr :← expr ('‥' | '…') expr
#
# EXPECTED OUTPUT:
# 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5
#
# BACKEND:
# Cross-ref for/intervallum.fab and operatores/membership-range.fab.
main {
mut int exclusivus ← 0
for range 0 ‥ 5 let _ {
exclusivus ↑
}
# 5 iterations
print exclusivus
mut int inclusivus ← 0
for range 0 … 5 let _ {
inclusivus ↑
}
# 6 iterations
print inclusivus
}Expected output:
5
6
radix/corpus/reference/operator-group/range/ranged-sugar-inf.fab (canonical · operator-group)#
inf is trapping<inf, -∞ … ∞>: no bound on either side, so the range spelling is the bare inf type.
# =============================================================================
# trapping<inf, -∞ … ∞> — the unbounded integer written with its range
# =============================================================================
#
# What this teaches:
# • \`inf\` has no bound on either side, so \`-∞…∞\` writes exactly that and the
# result is the bare \`inf\` type: no conversion, no check.
# • \`∞\` and \`-∞\` mean "no bound on this side" in a range; they are never an
# integer value. A finite end on \`inf\` (\`trapping<inf, 0 … ∞>\`) is refused.
#
# Common mistakes:
# • Writing a finite range on \`inf\` to bound it; use a bounded width.
#
# See also: inf, ranged-sugar
# =============================================================================
main {
let trapping<inf, -∞ … ∞> big ← 18446744073709551616
let inf same ← big
print same
}Expected output:
18446744073709551616
radix/corpus/reference/operator-group/range/ranged-sugar.fab (canonical · operator-group)#
The existing store-policy spellings are the whole-width range: saturating<u8> is saturating<u8, 0 … 255>, u8 is trapping<u8, 0 … 255>.
# =============================================================================
# ‥ … — the existing spellings are the whole-width range
# =============================================================================
#
# What this teaches:
# • `saturating<u8>` is `saturating<u8, 0 … 255>`, `wrapping<u8>` is
# `wrapping<u8, 0 … 255>`, and bare `u8` is `trapping<u8, 0 … 255>` (`inf` is
# `trapping<inf, -∞ … ∞>`, see `ranged-sugar-inf`). One type each, so a value
# moves between the two spellings with no conversion and no check.
# • `‥` is half open and `…` is inclusive: `0‥256` and `0…255` are the same
# range for a u8.
# • A half-open range holds its low end and stops before its high end:
# `-2‥3` holds -2 to 2.
#
# Common mistakes:
# • Expecting the two spellings to be different types — they are one.
# • Writing only one end of a range. Both ends are always written.
#
# See also: saturating, wrapping, trapping
# =============================================================================
fn through(saturating<u8, 0 ‥ 256> a) → saturating<u8> {
return a
}
main {
let saturating<u8> s ← 300
let saturating<u8, 0 … 255> sr ← s
print through(sr)
let trapping<u8, 0 … 255> t ← 200
let u8 plain ← t
let trapping<u8> named ← plain
print named
let wrapping<u8> w ← 300 ↦ wrapping<u8>
let wrapping<u8, 0 … 255> wr ← w
print wr
}Expected output:
255
200
44
radix/corpus/reference/reject/range/ranged-empty-reject.fab (supporting · reject)#
A range that holds no value is refused: 5‥5 is empty, 5…5 holds exactly 5.
# =============================================================================
# ‥ — an empty range holds no value
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • The half-open `5‥5` stops before the value it starts at, so no value is
# inside it. The inclusive `5…5` holds exactly 5.
#
# Common mistakes:
# • Reading `lo‥hi` as inclusive; `‥` is half open, `…` is inclusive.
main {
let saturating<u32, 5 ‥ 5> x ← 1
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-float-width-reject.fab (supporting · reject)#
A ranged store policy takes an integer width: saturating<f32, 0 ‥ 8> is refused.
# =============================================================================
# ‥ — float widths take no range yet
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • Ranges are integer only. Float, decimal and `inf` widths keep their whole-width
# policies (`saturating<f32>` clamps nothing; `inf` has no bound).
#
# Common mistakes:
# • Expecting a float range to clamp the way an integer range does.
main {
let saturating<f32, 0 ‥ 8> x ← 1
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-named-bound-reject.fab (supporting · reject)#
A range end is a literal integer or ∞ for now: a named or run-time bound is refused by name.
# =============================================================================
# ‥ — a range end is a literal integer or ∞
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • The checker can only prove that a range fits its width when both ends are
# known at check time. A name or a `size` extent as an end parses and is
# refused by name, so the form can arrive later without changing what is
# legal today.
#
# Common mistakes:
# • Writing the extent of a run-time buffer as an end (`0‥n`); spell the
# literal end, or keep the value in a wider cell and check it explicitly.
fn bound(int n) → void {
let saturating<u32, 0 ‥ n> x ← 1
print x
}
main {
bound(8)
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-open-end-reject.fab (supporting · reject)#
Both ends of a range are always written: 0… alone is refused, 0…∞ says there is no high end.
# =============================================================================
# ‥ — a one-sided range is a parse error
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • Both ends are always written, so a range never reads as half a range.
# "No bound on this side" is `∞` or `-∞`, written out.
#
# Common mistakes:
# • Dropping the high end to mean "no limit"; write `0…∞` instead.
main {
let saturating<u32, 0 … > x ← 1
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-outside-width-reject.fab (supporting · reject)#
A range must fit its width: trapping<u8, 0 ‥ 300> is refused because a u8 stops at 255.
# =============================================================================
# ‥ — the range must fit inside the width
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • The width is explicit and the range refines it, so an end past the width is
# an error at check time (a half-open `0‥256` is the last one a u8 accepts).
#
# Common mistakes:
# • Using a range to widen a width; pick a wider width such as `u16` instead.
main {
let trapping<u8, 0 ‥ 300> x ← 1
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-policy-word-reject.fab (supporting · reject)#
A range needs its store-policy word: u8<0‥8> is refused, saturating<u8, 0 ‥ 8> is the spelling.
# =============================================================================
# ‥ — a bare width marker takes no range
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • A range refines a store policy, so the policy word comes first: `saturating`,
# `trapping` or `wrapping`. A bare width like `u8` names the default policy and
# has no second argument.
#
# Common mistakes:
# • Writing `u8<0‥8>` hoping the bare marker means trapping; write
# `trapping<u8, 0 ‥ 8>` instead.
main {
let u8<0‥8> x ← 1
print x
}Expected: compilation rejects this example.
radix/corpus/reference/reject/range/ranged-unsigned-negative-reject.fab (supporting · reject)#
An unsigned width cannot start below zero: trapping<u8, -1 ‥ 5> is refused.
# =============================================================================
# ‥ — an unsigned width has no negative values
# =============================================================================
# This exemplum intentionally fails to compile.
#
# What this teaches:
# • Unsigned widths need a low end of at least 0; signed widths take negative
# ends (`trapping<i8, -1 ‥ 5>` is legal).
#
# Common mistakes:
# • Using an unsigned width for an offset that can go negative; use a signed width.
main {
let trapping<u8, -1 ‥ 5> x ← 1
print x
}Expected: compilation rejects this example.