def

def (value name --)

Inputs

Outputs

def creates one local or field from a stack value and a known Text name.

The name is taken directly from name as known Text and does not consume the name stack.

Pending specifiers from overload, private, and virtual apply to the created local and are then cleared.

Behavior

Example

{} 0 {} [
  pendingLabel:
  7 "x" def
  8;
  virtual 4 "staticValue" def
  5 "ordinaryValue" def
  1 "latest" def
  2 "latest" def

  "-- def uses its own text name --" printCompilerMessage
  x printStack _:;

  "-- pending label still used by ; --" printCompilerMessage
  pendingLabel printStack _:;

  "-- static local --" printCompilerMessage
  staticValue printStack _:;

  "-- In-place local after static def --" printCompilerMessage
  ordinaryValue printStack _:;

  "-- latest local wins --" printCompilerMessage
  latest printStack _:;

  "-- named fields created by def --" printCompilerMessage
  { 100 "score" def 2 "level" def } printStack _:;

  "-- duplicate field names created by def --" printCompilerMessage
  { 1 "x" def 2 "x" def } printStack _:;

  "-- unnamed items created by def --" printCompilerMessage
  { 1 "" def FALSE "" def } printStack _:;

  0
] "main" exportFunction

Expected Output During Compilation

-- def uses its own text name --
7 Cref
-- pending label still used by ; --
8 Cref
-- static local --
4
-- In-place local after static def --
5 Cref
-- latest local wins --
2 Cref
-- named fields created by def --
{
  score: 100;
  level: 2;
}
-- duplicate field names created by def --
{
  x: 1;
  x: 2;
}
-- unnamed items created by def --
(1 FALSE)

See also