class_add_ivar

Gamma: Version 4.0
Prev |  | Next

# `class_add_ivar`

`class_add_ivar` -- adds an instance variable to a class.

## Syntax

    class_add_ivar (class, variable, init_value?, type?)

## Arguments

`class`

  A class.

`variable`

  A symbol to be used as the instance variable name.

`init_value`

  Any Gamma or Lisp expression to be used as an initial value.

`type`

  An integer number which will be stored with the instance variable. This type is not used by the interpreter, and may be anything.

## Returns

The value of the `init_value`.

## Description

This function dynamically adds an instance variable to a class. All instances of that class which are created after this call will contain this instance variable. All instances created before this call will not contain this instance variable. This function is typically called on a class before any instances are created. It is too late to call this function within an instance constructor. All subclasses of this class will inherit the new instance variable. If the ivar already exists on the class, the only effect of this function is to change the default value.

## Example

| This example is based on the class and method developed in `method`. The instance sqB does not have "color" as an instance variable because it was created before the instance variable "color" was added.

        Gamma> class_add_ivar(Square, #color, "red", 12);
        "red"
        Gamma> sqC = new(Square);
        {Square (color . "red") (length) (sides . 4)}
        Gamma> sqB;
        {Square (length . 3) (sides . 4)}
        Gamma>

## See Also

`instance_vars`

Prev | Home | Next
`class_add_cvar` | Up | `class_name`

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.