ref
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Introduces borrowed iteration or borrowed parameters.
Syntax: ref <expression>
Category#
iteration
Related#
Examples#
radix/corpus/de/de.fab (canonical · keyword)#
Introduces borrowed iteration or borrowed parameters.
# =============================================================================
# ref — Introduces borrowed iteration or borrowed parameters.
# =============================================================================
#
# What this teaches:
# • Borrowed iteration — `for ref <expr> const <ident> { ... }` iterates over a collection without taking ownership.
# • Tabula key iteration — iterating over map keys via borrowed iteration.
# • Index iteration — iterating over list indices via `ref`.
#
# Common mistakes:
# • Passing a `ref` parameter to an `mut` or `from` position — mode mismatch; cannot pass a read-only reference where mutation or consumption is expected (SEM057).
#
# See also: from, in
# =============================================================================
# ref — borrowed iteration / parameters
#
# GRAMMAR:
# forDeStmt :← 'for' 'ref' expr 'const' ident block
#
# EXPECTED OUTPUT:
# Scalar stdout smoke (see body).
#
# BACKEND:
# Cross-ref: for/de.fab.
#
main {
const map<string, int> persona ← { "nomen": 1, "aetas": 30 }
for ref persona const clavis {
print clavis
}
const list<int, 3> numeri ← [10, 20, 30]
for ref numeri const index {
print index
}
}radix/corpus/itera/de.fab (canonical · keyword)#
Introduces borrowed iteration or borrowed parameters.
# =============================================================================
# ref — Introduces borrowed iteration or borrowed parameters.
# =============================================================================
#
# What this teaches:
# • borrowed iteration (`for ref`) over map keys and list indices — iterates without consuming the collection
# • accessing values by key or index within a borrowed loop
#
# Common mistakes:
# • Using ref (key/index iteration) when from (value iteration) is needed, or vice versa — ref iterates keys/indices, from iterates values.
#
# See also: from, in
# =============================================================================
# for ref — for-in key and index iteration
#
# for ref <object> const <key> { <body> }
# for ref <list> const <index> { <body> }
#
# GRAMMAR:
# forDeStmt :← 'for' 'ref' expr 'const' ident block
#
# EXPECTED OUTPUT:
# none — stdout not pinned for this exemplum.
main {
# Iterate over map keys
const map<string, string> persona ← { "nomen": "Marcus", "urbs": "Roma" }
for ref persona const clavis {
print clavis
}
# Access values using the key
for ref persona const clavis {
print clavis, persona[clavis]
}
# Iterate over list indices
const list<int, 3> numeri ← [10, 20, 30]
for ref numeri const index {
print "index: § valor: §"(index, numeri[index])
}
# With numerus-valued map
const map<string, int> forma ← { "alpha": 1, "beta": 2 }
for ref forma const k {
print k
}
}