[ Term Manipulation | Reference Manual | Alphabetic Index ]

# add_attribute(?Var, ?Attribute, +AttrName)

Add dynamically an attribute to a variable.

Var
  Any term.
Attribute
  Any term.
AttrName
  Integer or atom.

## Description

Adds an attribute with name AttrName and value Attribute to a variable. There must have been a preceding attribute declaration (meta_attribute/2) for AttrName.

If Var is a free variable, it will be turned into an attributed variable with a single attribute Attribute whose name is AttrName. If Var is already an attributed variable with other attributes, then the new attribute will be added to it.

Otherwise, if the attribute slot for AttrName is already occupied, or Var is already instantiated, then a new attributed variable with the single attribute Attribute is created, and subsequently unified with Var.

If AttrName is an atom, the attribute corresponds to a previous meta_attribute declaration. If AttrName is an integer, it is directly used as an attribute index slot (this is mainly used by the compiler to resolve attribute references at compile time).

### Modes and Determinism

- add_attribute(?, ?, +) is semidet

### Fail Conditions

Fails if Var is not a free variable and its unification with the new attributed variable fails

### Exceptions

(270) undefined variable attribute
  There is no attribute declared as AttrName.
(6) out of range
  AttrName is an integer, but not a valid attribute index

## Examples

    [eclipse 6]: meta_attribute(extra, []).

    yes.
    [eclipse 3]: add_attribute(X, a, extra), printf("%QPMw", X).
    X{suspend : _g270 , extra : a}
    X = X
    yes

## See Also

meta_attribute / 2, add_attribute / 2