Comments on: Extending Objects and Contexts
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: firstname.lastname@example.org 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: email@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:
insert user [email: firstname.lastname@example.org]
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).
Post a Comment:
You can post a comment here. Keep it on-topic.
Updated 8-Jun-2023 - Copyright Carl Sassenrath - WWW.REBOL.COM - Edit - Blogger Source Code