yield
Translation status: English reader-locale proof. Term names and code fences follow the en pack; supporting prose may still be English.
Yields one value from a generator.
Aliases: yield
Syntax: yield <expression>
Category#
async
Related#
Examples#
radix/corpus/reference/keyword/cede/cede.fab (canonical · keyword)#
Yields one value from a generator.
# =============================================================================
# yield — Yields one value from a generator.
# =============================================================================
#
# What this teaches:
# • Yield — `yield <expression>` emits one item from a `generator` or `async_generator`
# callable
# • Async values use `await_const`, `await_var`, `return_await`, or `await`; `yield`
# is not an await form
#
# Common mistakes:
# • using `yield` outside a generator body
#
# See also: cursor, generator, async_generator
# =============================================================================
# yield — yield from a stream function
#
# fn stream() generator → T { yield value }
#
# GRAMMAR:
# yieldStmt :← 'yield' expr
#
# EXPECTED OUTPUT:
# [1, 2]
#
# BACKEND:
# Cursor stream materialization smoke.
fn stream() generator → int {
yield 1
yield 2
}
main {
print stream()
}Expected output:
[1, 2]
radix/corpus/reference/reject/cede/cede-outside-generator-reject.fab (supporting · reject)#
Yielding needs a generator body; an ordinary function cannot emit items.
# =============================================================================
# yield — yielding outside a generator body is a check error
# =============================================================================
# `yield` (yield) emits one item from a generator callable (`generator` or `async_generator`).
# An ordinary function has no caller consuming a sequence, so the statement is
# refused before any code is emitted.
fn f() → int {
yield 1
return 2
}
main {
print f()
}Expected: compilation rejects this example.