Adding functions to an object.
[1/6] from: arolls:idatam:au at: 27-Feb-2002 0:20
This is a question only for people who have finished their bindology degrees. I have made an include function, for the purpose of "including" functions from other scripts in another script. It works great, except I want it to determine which context it is in and insert the new functions there. eg. ---------------------- ; library.r context [ func-one: func [... definition... func-two: func [... definition... ] ----------------------- ; Main Code ; this sets func-one and func-two in the global context ; to the same-named functions in the context returned ; by DOing %library.r include [%library.r [func-one func-two]] ; (this works) ; now, same again except I want the functions to be ; added to the object. o: make object! [ blah: none include [%library.r [func-one func-two]] ] ; so I should now be able to access the functions in o, o/func-one o/func-two ---------------------- I am trying to pass in the dummy word 'blah to the include function, so bind can use it as the known word. But.... <drum roll...> I am having trouble. :) I have been referring to Ladislav's "Words... Contexts" document but I am making slow progress. I am using Ladislav's in-object? function to determine the object from 'blah, but how to add the functions to it? Can anyone show an example of how to do this? I thought of using make, with the object as a template, but that makes a new object. I want to modify the existing object in place. Is that possible? Also, is it necessary to pass a known-word in to my include function? Perhaps there is a way to determine if include was called from within a context by checking if 'self is defined somehow? (I tried this but that's certainly not proof it can't be done.) Anyone have any ideas? If I can get it to work like this I'll be real happy! Regards to all, Anton.
[2/6] from: rotenca:telvia:it at: 26-Feb-2002 17:21
> This is a question only for people who have > finished their bindology degrees.
> I thought of using make, with the object as a template, but > that makes a new object. I want to modify the existing > object in place. Is that possible?
No. Only the global context can be extended. But you could make: 1) something like your library file:
>> x: "context [f1: func [x][x]]"
== "context [f1: func [x][x]]" 2) something like your 'include func (without the library file name):
>> include: func [arg][lib: do x set arg/1 get in lib arg/1]
3) now use it:
>> o: context [f1: none include [f1]]
4) the result:
>> probe o
make object! [ f1: func [x][x] ] The body of the included function remain bind to the original context. --- Ciao Romano
[3/6] from: greggirwin:mindspring at: 26-Feb-2002 9:39
Hi Anton, << I thought of using make, with the object as a template, but that makes a new object. I want to modify the existing object in place. Is that possible? >> LOAD and DO bind words to the global context automatically, and I don't know of any way around that, assuming you're using LOAD or DO. My workaround was to load what I wanted as data, and append it to a block, which I then used as a spec block with MAKE, to create an object. --Gregg
[4/6] from: al:bri:xtra at: 27-Feb-2002 17:55
> ; now, same again except I want the functions to be > ; added to the object. > o: make object! [ > blah: none > include [%library.r [func-one func-two]] > ]
Basically, the reason why your code is failing, is because 'do only returns the last value in the block or file it processes. So the function! value referred to by 'func-one, never appears inside the object. One simple way of do-ing it, is like this: ; Func-one.r func   ; Main code o: make object! [ Func-one: do %Func-one.r ] Another way, if objects are important, is: ; Object-code.r make object! [ Local: none Func-one: func   ] ; Main code o: make do %Object-code.r [ MyCustomValue: 123 ; and so on. ] Note that in both cases, only one function! or object! value is in the included file. If you need more, use more files, or use an object. I hope that helps! Andrew Martin Rebol-bound... ICQ: 26227169 http://valley.150m.com/
[5/6] from: al:bri:xtra at: 27-Feb-2002 18:12
> I want to modify the existing object in place. Is that possible?
Not directly. But try something like this:
>> o: make object! [
[ Name: "My Object!" [ Extensions: make block! 10 [ ]
>> repend o/Extensions bind ['Test func [x][print [x name "!!"]]] in o 'self
== [Test func [x][print [x name "!!"]]]
>> o/extensions/test 4
4 My Object! !!
** Script Error: name has no value ** Near: name
** Script Error: test has no value ** Near: test
I hope that helps! Andrew Martin ICQ: 26227169 http://valley.150m.com/
[6/6] from: arolls::idatam::com::au at: 28-Feb-2002 5:27
Thankyou all for your ideas, I will see what I can come up with. If you want to look at my include func, I uploaded it here: http://anton.idatam.com.au/rebol/library/include.r This was the closest to my situation. I was encapsulating all my library functions in an object.
> Another way, if objects are important, is: > ; Object-code.r
<<quoted lines omitted: 10>>> included file. If you need more, use more files, or use an object. > Andrew Martin
I wish the anonymous context from which the include function is called could be accessible and modifiable from within the include function. Nevermind too much, the include function is quite usable as is. Just it would be nice... Anton.
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted