virtual (--)
Inputs
- None.
Outputs
- None.
virtual is the declaration modifier that marks the next successfully created local as static.
The name is historical. The resulting field-storage property is what fieldIsVirtual reports. The schema-level Meta test is virtual?.
The builtin sets a pending static specifier in the current scope. That pending specifier is consumed by the next successful local creation through NAME: ...; or def.
Known issue: a non-Meta uniform Dict that holds a static field can compile to invalid LLVM IR; Clang reports constant expression type mismatch. The Dict also occupies more storage than storageSize reports, so code that relies on that size can read or write the wrong storage. With an unknown ordinal at or after a static field, @, !, fieldRead, and fieldWrite select a later item or storage past the solid fields. With mutable Ref items, @ and fieldRead with any unknown ordinal can instead make mplc abort with the internal assertion Item is Meta. Give the static field, or an added one, another schema, such as 1n8 beside Int32 fields.
Behavior
- If the static specifier is already pending, the builtin raises a duplicate-specifier error.
- After the next successful local creation, the pending static flag is cleared.
- Other operations do not consume the pending static flag.
- A pending static flag is invalid for
exportFunction,importFunction, andimportVariable. - If the scope ends before the pending static flag is consumed, the program is invalid.
- Pending
overloadorprivatespecifiers coexist with the pending static flag. - This does not make the resulting object Meta.
Example: values without storage
virtual makes the next created local or field static: it reads as a plain value and has no storage according to fieldIsVirtual, while the Int32 local and field shown are still not Meta according to virtual?.
{} 0 {} [
virtual staticLocal: 1;
dict: { virtual 2 "staticField" def };
"-- values created with virtual are static, not meta --" printCompilerMessage
staticLocal printStack _:;
staticLocal virtual? printStack _:;
dict.staticField printStack _:;
dict.staticField virtual? printStack _:;
"-- fieldIsVirtual sees static field storage --" printCompilerMessage
dict 0 fieldIsVirtual printStack _:;
0
] "main" exportFunction
Expected Output During Compilation
-- values created with virtual are static, not meta --
1
FALSE
2
FALSE
-- fieldIsVirtual sees static field storage --
TRUE
See also
NAME: ...;node—Pushes a name onto the name stack and creates a local by consuming that name and one data-stack object.defbuiltin(object name --)Creates a named local or field using a known Text name.exportFunctionbuiltin(inputsSchema output options callable name --)Exports a named function built from a Block and a signature; a pendingvirtualspecifier is invalid there.importFunctionbuiltin(inputsSchema output options name --)Declares or imports a named function with the supplied signature; a pendingvirtualspecifier is invalid there.importVariablebuiltin(schema name --)Creates a local that refers to external variable storage; a pendingvirtualspecifier is invalid there.fieldIsVirtualbuiltin(schema ordinal -- hasNoStorage)No-storage test for one selected item.staticbuiltin(--)Reserved builtin name with no standalone effect.virtual?builtin(schema -- isMeta)Historical Meta-schema test.