Records, variants, resources, and streams¶
Beyond plain values such as numbers and text, an interface describes structured data, things a caller holds across calls, and results that arrive over time. XCI has one construct for each, and each one appears in the code generation produces for your block.
Records and variants¶
A record is a named group of fields, such as a charge request with an account and an amount. A variant is a named choice between cases, where a value is exactly one case, such as a payment by card or by bank transfer. Each member has a number, a name and a type.
The ledger block's charge request, as generated for your Python code:
@dataclass(frozen=True, slots=True)
class ChargeRequest:
account: str # field id 1
amount: int # field id 2
CHARGEREQUEST_FIELD_IDS = {'account': 1, 'amount': 2}
A record type has one shape everywhere. When two methods use
acme.billing.ChargeRequest, both describe the same fields, and a definition
that gives it two shapes stops compilation:
Field numbers are permanent¶
A field's number identifies it in the encoded value; its name is for people. A removed field's number is retired, and a record that reuses a retired number is refused. Reusing one would make data written with the old field read as the new field, with no error. See Interface evolution and compatibility.
Resources¶
A resource is something a caller holds across several calls, such as an open session or a cursor. The interface states how it is handed out and how many calls may use it at once:
owned gives the resource to the caller, who closes it; borrowed lends it for
one call. exclusive admits one call at a time; shared admits any number at
once. Whether a session is safe to use in parallel is known only to its author,
so it is declared.
The resource itself stays in the block that created it. A caller holds a handle, and each call on the resource runs in the owning block. See Cells.
Results that arrive over time¶
Each method declares how its result arrives:
value: one result when the call completes.future: one result the caller waits for, and may cancel.stream: items that keep arriving until the stream ends.
Delivery is declared apart from the result type: a list returned at the end and the same items streamed one by one have the same type, and different timing, back-pressure and failure behaviour.
A method can also return a reference to something the kernel holds, such as
a task, a stream or an artifact. The ledger's charge returns a task reference
through a future: the caller waits on the task rather than on a value. The
full set of types, delivery values and reference kinds is in the
XCI source reference.