add_set_function

Gamma: Version 4.0
Prev |  | Next

# `add_set_function`

`add_set_function` -- sets an expression to be evaluated when a given symbol changes value.

## Syntax

    add_set_function (symbol, s_exp)

## Arguments

`symbol`

  A symbol.

`s_exp`

  Any Gamma or Lisp expression.

## Returns

t

## Description

This function binds an expression to be evaluated whenever the value of the `symbol` changes. This expression is available globally, so if the value of the `symbol` changes during a change in scope, the expression will be evaluated. All changes in that sub-scope will trigger new evaluations of the expression. This can be used to automatically maintain consistency between the program and a real-time datahub, or to implement forward chaining in calculation rules. The expression will not be evaluated if the new value is eq to the previous value.

When a set expression (the `s_exp`) is being evaluated the special variables `this`, `value` and `previous` are all bound:

- `this` The symbol whose value has changed.

- `value` The current value of this as a result of the change.

- `previous` The value of this immediately prior to the change.

## Example

        Gamma> b = 5;
        5
        Gamma> add_set_function(#b,#princ("changed\n"));
        (princ "changed\n")
        Gamma> b = 4;
        changed
        4
        Gamma>

The following code automatically sounds an alarm whenever a `computed_tank_level` rises above 10000 and silences the alarm whenever it drops below 10000. The datahub is automatically updated to maintain the same value as the Gamma task.

        function send_to_datahub (point)
        {
        	write_point(point, value);
        }

        function check_alarm (value)
        {
        	if (value == 1)
        		sound_alarm();
        	else
        		silence_alarm();

        	send_to_datahub(this);
        }

        function check_tank_level (depth)
        {
        	if (depth > 10000)
        		high_alarm = 1;
        	else
        		high_alarm = 0;

        	send_to_datahub(this);
        }

        add_set_function(#high_alarm, #check_alarm(high_alarm));
        add_set_function(#computed_tank_level,#check_tank_level(computed_tank_level));

## See Also

`when_set_fns`, `remove_set_function`

Prev | Home | Next
Events and Callbacks | Up | `flush_events`

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