[REBOL] Re: How do I dynamically extend an object! instance
From: g:santilli:tiscalinet:it at: 10-Sep-2000 18:34
Hello [bobr--dprc--net]!
On 10-Set-00, you wrote:
b> object-addword: func [
b> { add a word only if it is not already there,
b> returns a new instance of the object
b> examples
b> myobj: object-addword myobj emailaddr
b> dbrecord: object-addword/initial dbrecord areacode
b> 978
b> }
I'd suggest:
object-addword: func [
object [object!]
'word [word!]
/initial value [any-type!]
] [
either not found? in object word [
make object reduce [to-set-word word value]
] [
object
]
]
>> probe o
make object! [
a: 1
]
>> probe object-addword o b
make object! [
a: 1
b: none
]
>> probe object-addword/initial o b 2
make object! [
a: 1
b: 2
]
>> probe object-addword/initial o a 2
make object! [
a: 1
]
b> - can it be done without creating a new instance?
I'm afraid not, with the current implemenation of REBOL.
b> - can a corresponding function for removing a word
b> from an object be written without evaluating
b> all the other words/elements?
I can only think of:
remove-word: func [
object [object!]
'word [word!]
/local words values
] [
if found? in object word [
words: first object
values: second object
object: clear []
foreach obj-word next words [
if obj-word <> word [insert object to-set-word obj-word]
]
insert tail object none
object: make object! object
remove at values index? find words word
set next bind first object in object 'self next values
]
object
]
HTH,
Gabriele.
--
Gabriele Santilli <[giesse--writeme--com]> - Amigan - REBOL programmer
Amiga Group Italia sez. L'Aquila -- http://www.amyresource.it/AGI/