[REBOL] How do I dynamically extend an object! instance
From: bobr:dprc at: 9-Sep-2000 23:50
I have an existing dataase
of saved objects which I wish to add fields to (IE add words:).
I probably only want to add the words if I absolutely must
in order to keep size down. I also may already
have added a particular word to an object instance and dont wish to
overwrite the value already associated with that word.
Here is what I have so far.
questions follow below.
object-addword: func [
{ add a word only if it is not already there,
returns a new instance of the object
examples
myobj: object-addword myobj emailaddr
dbrecord: object-addword/initial dbrecord areacode 978
}
o [object!] "the object to have a word added"
'w1 [any-word!] "the word to add"
/initial
vdef [any-type!] "provide initial value for the word"
/local
mb "mini block"
] [
if not find (first o) w1 [
; try to emulate: set/any in o w1 none
mb: do rejoin [ {[} :w1 {: none ]} ]
o: make o mb
if initial [ set/any in o w1 vdef ]
]
return o
]
;-------- for discussion:
- can this be written more succinctly yet not hardcode
anything about the object?
- can it be done without creating a new instance?
- can a corresponding function for removing a word
from an object be written without evaluating
all the other words/elements?
I have tried several arrangements for the arguments
and names for the function. I have settled on
object-addword rcvrobj operand
- are the precedents for putting the word operand first?
- since, from context, you can tell which argument
is the object and which is simply a word
which may need to be added to the object,
why not make the function figure out
which argument is which type and do the right thing
regardless of how it is called?
can this be coded without resorting to second-level functions?
- is a better name for the function possible?
I have considered 'object+ and 'object+word as potential names.
Is there a precedent that I have missed?
;# mailto: [bobr--dprc--net]