The concept of SetFunctions
You know what a setword is:
word: 10
It's the syntax REBOL uses to define a word's value.
A setfunction is function that gets called when that set action take place. A setfunction for the above example might check the range, datatype, or keep track of other details.
The concept of a setfunction 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 macrocomponent 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 setfunction method because it does offer great value. A setfunction 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 setfunctions. If it were really easy, we'd have them. The problem is that setfunctions 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
setvalue: func [new] [value: new]
]
And you would write:
obj/setvalue 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 setfunction 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
