The concept of Set-Functions
You know what a set-word is:
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:
But, what you really want to write is:
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.