[REBOL] Re: Object lesson, please?
From: SunandaDH:aol at: 24-Oct-2003 4:34
Gabriele:
> 1. you can do this:
>
> >> original: context [a: 1 b: 2]
> >> template: context [a: b: c: none]
> >> probe changed: make template original
Thanks, Gabriele. The one thing I hadn't thought of trying was context -- the
word had slipped from my mental context.
I had to change the code around as context takes a block, not an object --
hence the the "content third xxx" to grab the block inside the object.
But even then a simple conversion of the code didn't work -- I ended up with
inner objects still being cloned.
I've had to do a copy/deep of the originals to break the cloning. That's
almost as unsatisfactory as the original code. Here's the latest version, with a
simpler set of test data:
;;========== the function ==========
Rebol []
update-object: func [obj [object!]
template [object!]
/local
obj-copy
template-copy
]
[
obj-copy: make object! copy/deep third obj
template-copy: make object! copy/deep third template
foreach field next first template-copy
[
;; add a field if it doesn't exist
if error? try [obj-copy/:field]
[
obj-copy: context join third obj-copy
[to-set-word field template-copy/:field]
]
;; recurse adding fields if field is an object
if object? template-copy/:field
[
obj-copy: context join third obj-copy
[to-set-word field update-object obj-copy/:field
template-copy/:field]
]
] ;; for
return obj-copy ;; no more do mold !
] ;; func
;; ========== Some test data data ==========
original-object: make object! []
template: make object! [a: make object! [b: 1]]
new-object1: update-object original-object template
new-object2: update-object original-object new-object1
print "status messages follow -- all say true if it's worked"
print not same? new-object1/a template/a
print not same? new-object1/a new-object2/a
print equal? mold new-object1 mold new-object2
=============
Sunanda.