# The concept of Set-Functions

 Carl Sassenrath, CTO REBOL Technologies 2-May-2006 21:02 GMT Article #0019

You know what a set-word is:

```word: 10
```

It's the syntax REBOL uses to define a word's value.

A set-function is function that gets called when that set action take place. A set-function for the above example might check the range, datatype, or keep track of other details.

The concept of a set-function is not new. In fact, it's an essential part of one of REBOL's grandparent languages: Self. (See Self: The Power of Simplicity as one of my favorites.)

Back in the early formative days of Self while I was at Apple, I use to hang out with David Ungar (Self's designer, at Stanford at the time) and talk language talk. I found it interesting that David wanted to eliminate state from Self by removing assignment. (Assignment is one of the classical headaches in pure functional languages.) He accomplished that by always using functions for both getting and setting values within a context (object).

The idea always appealed to me, but I felt it wasn't exactly practical. State is macro-component of nature (I did not say "component" here because I do not want to enter an endlessly deep discussion.) Elimination of state is a mathematical pursuit, but not necessarily practical. There are many useful features of state. A newspaper is very practical, but mathematically disfunctional.

When it came time to consider this approach in REBOL (more than a decade ago), I decided to keep it practical. However, secretly, I've always wanted to bring back the set-function method because it does offer great value. A set-function can precisely control how a word is set and what it is set to.

I should mention, however, it's not that easy to add set-functions. If it were really easy, we'd have them. The problem is that set-functions add another branch to the tree of REBOL language semantics. For example, how are set functions defined and how reflective are they?

Also, you can "sort of" do set functions today with code like this:

```obj: make object! [
value: 0  ; internal
set-value: func [new] [value: new]
]
```

And you would write:

```obj/set-value 10
```

But, what you really want to write is:

```obj/value: 10
```

where it passes through a function to do the set.

But then the question becomes, how do you refer to the set-function itself?

Ok, so that's something to think about. I'm not sure there is a good answer, or even if we need one. I've talked about this before, and not even suggesting that this be added to REBOL 3.0 (see prior article about cool language tricks).

I suppose I'm just trying to get you thinking more about it... Please post your comments.

21 Comments

Updated 15-Jul-2024 - Edit - Copyright REBOL Technologies - REBOL.net