patching an object
[1/7] from: dhsunanda::gmail::com at: 5-Jul-2007 8:44
Is there an *elegant* way of doing this?
I have an object that makes up an API:
my-api: make object! [
a: 1
b: 2
f1: func [] [print a + b]
]
my-api/f1
== 3 ;; as you'd expect
But I also have a set of patches that I want to apply
to the API to override various words:
patches: make object! [
b: 12
f1: func [] [print a - b]
]
But if I apply the patches......
my-api: construct/with third patches my-api
.......it does not bind the words as I'd hope:
my-api/f1
** Script Error: a has no value
** Where: f1
** Near: print a - b
***
One quick fix (given that this currently happens at application
start-up, so I do not need to worry about any persistent values)
is to mold and load:
my-api: first reduce load/all mold my-api
my-api/f1
== -11 ;; the correctly patched result
But I am looking for something a little more elegant and/or REBOLish.
Any ideas?
Thanks,
Sunanda.
[2/7] from: btiffin:rogers at: 6-Jul-2007 12:40
Sunanda; I'm probably 'not getting it', but
my-api: make my-api [b: 12 f1: [a - b]]
Cheers,
Brian
On Thursday 05 July 2007 03:44, Sunanda wrote:
[3/7] from: dhsunanda:gm:ail at: 9-Jul-2007 17:46
Brian:
> Sunanda; I'm probably 'not getting it', but
>
> my-api: make my-api [b: 12 f1: [a - b]]
Thanks for the reply.
The differences in your code are:
1. I was using two objects
2. f1 was a function in both cases
If I put those characteristics back, and then try your make trick
(rather than my original construct/with approach), I get....
my-api: make object! [
a: 1
b: 2
f1: func [] [print a + b]
]
patch: make object! [b: 12 f1: func [][print a - b]]
my-api: make my-api third patch
** Script Error: a has no value
** Near: print a - b
.... which is the original issue, restated.
***
This will work with your approach:
my-api: make my-api load mold third patch
But it hardly seems elegant to my mind.
Any other approaches out there?
Thanks,
Sunanda
[4/7] from: santilli:gabriele:gma:il at: 9-Jul-2007 19:33
2007/7/9, Sunanda <dhsunanda-gmail.com>:
> patch: make object! [b: 12 f1: func [][print a - b]]
Does patch have to be an object? If it's a block, make my-api patch should work.
Regards,
Gabriele.
[5/7] from: chris-ross:gill at: 10-Jul-2007 10:36
Hi Sunanda,
I think this depends on the Core version, but:
my-api: make my-api patches
- Chris
On Jul 5, 2007, at 2:44 AM, Sunanda wrote:
[6/7] from: pwawood::gmail at: 10-Jul-2007 19:24
Sunanda
I tried this :
>> my-api: make object! [
[ a: 1
[ b: 2
[ f1: func [] [print a + b]
[ ]
>> patch: make object! [
[ b: 12
[ f1: func [] [print a - b]
[ ]
>> my-api: make my-api patch
>> source my-api
my-api: make object! [
a: 1
b: 12
f1: func [][print a - b]
]
Seems to do what you want.
Cheers
Peter
On Tuesday, July 10, 2007, at 12:46 am, Sunanda wrote:
[7/7] from: dhsunanda:gma:il at: 7-Jul-2007 12:09
Brian:
> Sunanda; I'm probably 'not getting it', but
>
> my-api: make my-api [b: 12 f1: [a - b]]
Thanks for the reply.
The differences in your code are:
If I put those characteristics back, and then try your make trick
(rather than my original construct/with approach), I get....
my-api: make object! [
a: 1
b: 2
f1: func [] [print a + b]
]
patch: make object! [b: 12 f1: func [][print a - b]]
my-api: make my-api third patch
** Script Error: a has no value
** Near: print a - b
.... which is the original issue, restated.
This will work with your approach:
my-api: make my-api load mold third patch
But it hardly seems elegant to my mind.
Any other approaches out there?
Thanks,
Sunanda