fieldIndex (schema itemName -- ordinal)
Inputs
schema: Object or Ref whose dereferenced schema is Struct and whose item names are searched byitemName.itemName: Known non-NILTextitem name used for selection.
Outputs
ordinal: Known Int32 ordinal of the selected item.
Returns the selected item ordinal for a known, non-NIL Text item name in the dereferenced Struct schema; name-based field selection uses the same last-match rule as .NAME.
Only the schema participates. Current value contents do not participate.
Use fieldName to preserve the exact stored name at one ordinal and has for presence-only testing.
Behavior
- For Dict, the last item whose name matches
itemNameis selected. - For Dict, the empty name
""also matches unnamed items and selects the last unnamed item when one is present. - For List and Tuple, only the empty name
""is valid, and it selects the last item. - For an empty List or Tuple, no name is valid, including
"". - If no matching item exists, the builtin raises a compilation error.
Example
{} 0 {} [
mixed: { 1 "" def 2 "" def 3 "x" def 4 "x" def };
"-- fieldIndex returns the last match --" printCompilerMessage
virtual emptyKey: mixed "" fieldIndex;
emptyKey printStack _:;
mixed emptyKey fieldName printStack _:;
virtual xKey: mixed "x" fieldIndex;
xKey printStack _:;
mixed xKey fieldName printStack _:;
"-- earlier matches still exist --" printCompilerMessage
mixed fieldCount printStack _:;
mixed 0 fieldName printStack _:;
mixed 2 fieldName printStack _:;
"-- Tuple unnamed item --" printCompilerMessage
virtual tupleKey: (1 FALSE) "" fieldIndex;
tupleKey printStack _:;
(1 FALSE) tupleKey fieldName printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
-- fieldIndex returns the last match --
1
""
3
"x"
-- earlier matches still exist --
4
""
"x"
-- Tuple unnamed item --
1
""
See also
fieldCountbuiltin(schema -- itemCount)Schema item count and valid ordinal-range upper bound.fieldIsRefbuiltin(schema ordinal -- isRefLike)Ref-like test for one selected item schema.fieldIsVirtualbuiltin(schema ordinal -- hasNoStorage)No-storage test for one selected item.fieldNamebuiltin(schema ordinal -- itemName)Ordinal-to-name lookup that preserves the stored name at one selected ordinal.hasbuiltin(schema itemName -- hasName)Known-name presence test that does not choose an ordinal.schemaNamebuiltin(schema -- reportedName)Reported name of the dereferenced schema.