REBOL Technologies

Extending Objects and Contexts

Carl Sassenrath, CTO
REBOL Technologies
14-Dec-2005 18:05 GMT

Article #0233
Main page || Index || Prior Article [0232] || Next Article [0234] || 1 Comments || Send feedback

I've noticed that the topic of extending objects has come up again. I've talked about it before, but it is worth discussing.

In a nutshell, the idea is to support this feature:

user: make object! [
    name: "Fred Smith"
    city: "New York"
    age: 32
]

extend user [
    email: fred@example.com
    zip: 12345
]

Here extend add the new words and values to the user object (but, without creating a new object).

Of course, you can currently accomplish something similar with the line:

user: make user [
    email: fred@example.com
    zip: 12345
]

But, that creates an entirely new object.

So, the question is: do you want to be able to extend an object without creating a new one? And, what value does it offer?

I've been considering this question for many years. We often use REBOL's prototype objects for one-of-a-kind contexts, such as creating program modules (like system or system/view for example). Such contexts can be very large, and if you want to extend them with a few new words, using make seems like overkill. In addition, you need a way to replace all references to the older object with the new object. That may not be easy.

Adding extend to REBOL is almost trivial. In fact, the operation is just like an append (an insert) with a new bind over the entire object. This brings up a few questions:

  • Should we simply expand the definition of objects to include the insert datatype action abstraction?
insert user [email: fred@example.com]
  • Or, does the fact that the implementation requires a new bind operation dictate that a new function be used (e.g. extend)? Insert has never implied binding.

  • If we allow insert, do we allow remove or other series functions? How much do we want to "overload" the object model with series actions?
remove user 'age

So, these all produce similar results:

print select user 'name
print user/name
print get in user 'name

These are interesting and fun questions to consider, and I'm sure there are a few others as well. Perhaps, give them some thought this month as you sip your eggnog and relax by the fire... or BBQ shrimp and relax on the beach (for the many REBOLers down under).

1 Comments

Updated 7-Mar-2024   -   Copyright Carl Sassenrath   -   WWW.REBOL.COM   -   Edit   -   Blogger Source Code