virtual (--)

Inputs

Outputs

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

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