Every value that crosses the boundary is checked against the type the entry
declares. Nothing is coerced on the way in, and nothing is approximated on
the way out. This chapter lists what crosses and the rules each value
follows. It describes captain-sdk 0.7.0, which added lists, records and
options (pure profile §3.2).
Inputs
An argument's value is an InputValue. Each variant matches one kind of
Captain type, written as the artifact renders it in the entry's signature:
InputValue |
Against | Notes |
|---|---|---|
Bool(b) |
bool |
|
I32(n) |
i32 |
|
I64(n) |
i64 |
|
Decimal { mantissa, scale } |
Decimal |
the value is mantissa × 10^-scale: { mantissa: 1999, scale: 2 } is 19.99 |
Text(bytes) |
Text |
UTF-8 bytes |
Octets(bytes) |
Octets |
|
Symbol(name) |
Symbol |
the name without # |
Tagged { type_name, text } |
a tagged scalar type | type_name must be the parameter's type; the text is validated as that type |
Quantity { dimension, mantissa, scale, unit } |
a quantity type such as Duration |
dimension must be the parameter's type, and unit a unit of that dimension the artifact knows |
List(items) |
List[T] |
every item against T; an empty list is a list |
Record { type_name, fields } |
a struct of the artifact | see below |
Some(value) |
Option[T] |
value against T |
None |
Option[T] |
The rule is the checker's rule, applied at the boundary: the value's kind must be the type's kind.
- An
I64is not aDecimal, even when its value would fit. - A bare value is not
Some. ASome(None)is not anOption[Text]. Noneagainst a parameter that is not anOptionis refused.- A list is checked item by item, and a refusal names the item:
item 1.
Records
A record is nominal and closed:
type_namemust be the struct's exact name, as the signature writes it. One struct is not another, even with the same fields.- Every declared field must be given exactly once, by name. A missing, extra or duplicated field is refused.
- The fields may be given in any order. The SDK freezes them in the struct's declared order, so the same values always make the same arguments, and the same plan identity.
- Each field's value is checked against the field's declared type.
The test records_are_nominal_closed_and_exact refuses each of these, with
CAP-SDK-ARGUMENT-TYPE and a message naming the place:
| Given | Refused because |
|---|---|
a field unitprice for unitPrice |
OrderLine has no field unitprice |
only quantity |
field unitPrice is missing |
an extra note field |
OrderLine has no field note |
quantity twice |
given more than once |
a Note record where an OrderLine is wanted |
a Note record does not match |
an I64(3) as quantity |
OrderLine field quantity: the value kind does not match Decimal |
| one record where a list is wanted | a record does not match List[OrderLine] |
A record also needs its struct's layout in the artifact. The compiler writes
the layout of every type an exported signature reaches, so a struct that
only crosses the boundary, as in echo p: Pair -> Pair [ ret p. ], can be
handed in. An artifact built by an older compiler has only the structs its
procedures build or read; rebuild it, or planning refuses the record with
"artifact has no layout for …".
Planning checks; it does not guess. A host that holds data whose shape comes from somewhere else gives it a Captain struct of its own, generated if need be, rather than passing a loose map. Captain has no open or dynamic records, by design (DR-0033, decision 5).
What is not admitted
Secrets, grants, receipts, shapes, staged and symbolic values, generic
structs other than List and Option, and enum values other than
Option's are refused as inputs. A pure entry has no use for authority, and
the rest have no meaning outside a run.
Results
A completed run's value is an OutputValue. Scalars come back as the
variants of the same names: Bool, I32, I64, Decimal, Text,
Octets, Symbol, Tagged, Quantity. A procedure that returns nothing
gives Void. The rest:
| Captain value | OutputValue |
|---|---|
| a list | List(items) |
| a struct | Record { type_name, fields: [(name, value)] }, fields in declaration order |
Option's Some / None |
Some(value) / None |
a variant of another enum, such as Result's Ok and Fail |
Variant { type_name, variant, fields: [(name, value)] } |
| a shape | Shape([(label, value)]), each label optional |
| a rate | Rate { money, per } |
| a type reference | TypeRef(name) |
| a value of a type the artifact lists without a layout | Data { type_name, fields }, positional |
OutputValue::field(name) reads a record's or a variant's field by name, so
a host can walk a result without matching every level:
let failed = run(-1);
assert_eq!(
failed.field("err").and_then(|err| err.field("kind")),
Some(&OutputValue::Symbol("negative".into()))
);That is from the test other_variants_come_back_named_by_enum_and_variant,
which runs this entry:
module Sdk::Fixture.
export check n: i64 -> Result[i64] [
n < 0 ifTrue: [ ret Fail { err: Err { kind: #negative, message: "below zero" } }. ].
ret Ok { v: n }.
].check n: 4 comes back as Variant { type_name: "Result", variant: "Ok",
fields: [("v", I64(4))] }. check n: -1 comes back as the Fail variant,
whose err field is a Record of type Err.
A record goes in as it came out. The test
records_and_options_come_back_with_their_names returns a record holding a
record, an option and a list, and passes it back in, unchanged.
A secret, grant, receipt, staged or symbolic value at the result boundary
makes the outcome InvalidResult with CAP-SDK-RESULT-TYPE. It is never
approximated.
Quantities in results
A quantity's mantissa and scale in a result are in the dimension's
base unit, and unit names the unit the value carries. Going in, the
mantissa is in the unit named. So a Duration of 2min given as
Quantity { dimension: "Duration", mantissa: 2, scale: 0, unit: "min" } to
an entry that adds 5min comes back as mantissa: 420000000000 (seven
minutes, in nanoseconds) with unit: "min". Convert with the unit's factor
when you need the written form.
Before 0.7.0
Version 0.6 and earlier admitted scalar inputs only, and returned every
struct and every enum variant as positional Data. A host that matched
Data for a struct matches Record now. The result's canonical bytes, its
ResultId and the run record did not change: only the Rust projection did.