[REBOL] Re: Storing/loading object functions
From: knizhnik:garret:ru at: 7-Dec-2003 23:33
The problem is that I want to be able to store in database ANY object.
So, if I correctly understand you, the only possible solution in this
case is create string with definition of the object "make object! ..."
and then evaluate this string using "do".
It seems to me that performance of such solution will be awful.
Ok, then alternative is to require persistent object to contain "class:" attribute
which specifies prototype object. In this case I database do not need
to store/load methods. It will use prototype object for creation of
loaded instances. And one more question: I find out no way to locate
object by string except using "do". Is it the only possible solution
and how expensive it is? If it is really the only possible way to
locate the object by name and it is not very fast, then I will use
hash table to keep name->object mapping.
Thanks in advance
Sunday, December 7, 2003, 12:10:56 AM, you wrote:
VN> Am Samstag 06 Dezember 2003 19:20 schrieb Konstantin Knizhnik:
>> GI> Hi Konstantin,
>> KK>> I am developer of object oriented database for dynamic languages
>> KK>> (www.garret.ru/~knizhnik/dybase.html)
>> KK>> Currently it supports PHP, Python and Ruby. Now I am going to develop
>> KK>> Rebol API for DyBASE.
>> GI> That's great! I think someone mentioned DyBase here not too long ago.
>> GI> Christian already answered your question, but please don't hesitate to
>> GI> ask more questions here. I think a lot of us would be glad to help,
>> GI> and there are some real gurus here so, if it can be done, someone here
>> GI> will know how to do it.
>> So I have once again to ask for a help.
>> Unlike all other languages with OO extensions I have deal before, in
>> Rebol there are completely no classes - objects are used as
>> prototypes to create other objects. Certainly it is very flexible
>> approach and in most situation programmer see no difference between
>> using class or prototype object.
>> But to be able to store and load objects from the database,
>> I need to store/load not only object values but also objet functions
>> (which actually are treated as normal object fields with function
>> value). I have never faced with this problem before (in PHP, Python or
>> Ruby) API, because it was always assumed that application itself keeps
>> method definition. And if method definition is changed it is likely to be
>> changed for all objects of this class and not only for newly created
VN> You want to store the class for the object or indivuidual functions?
VN> If class:
>> But in Rebol there is no class. And looks like there is no way to find
>> out prototype object for the particular object instance.
VN> except you store the classname in the object
VN> a-class: make object! [type: "a-class" a: none]
VN> a more efficient way is to use a method-object, like faces do.
VN> then you have
VN> a-handler: context [
VN> type: "a-handler"
VN> add: func [this increment] [this/value: this/value + increment]
VN> ; ^ note we pass 'this explicitely
VN> a-class: context [handler: a-handler a: none]
VN> a-object: make a-class [value: 5]
VN> a-object/handler/add a-object 3
VN> the call-syntax is not very handy,
VN> but complete /view-styles are based on this system.
VN> Usually there is a dispatch-function, so that one can write
VN> ao-add: func[this incr][this/handler/add this 3]
VN> ao-add a-object 4
VN> see 'do-face in /view,
VN> or 'show, which dispatches to face/feel/redraw and does some more work.
VN> note i dropped the string-encoding for data here, to have something to add.
>> So it seems to me that the only possible solution in Rebol is to store
>> functions inside database. So my questions are
>> 1. May be I missed something and there is some other way of setting
>> object functions for loaded persistent object?
>> 2. How it is possible to store function? It seems to be two possible
>> solutions: represent it as series or convert it to string.
VN> str probe mold :append
VN> append2: do str
VN> ; the ":" is important here!
VN> ; binding may make problems. Keep the functions better outside
VN> ; the data-object. see handler-approach.
VN> (if you really want to store source in db)
>> I do not know how to implement any of them:
>> >> probe :f
>> func [var3: now/time]
>> >> to-string :f
>> == "?function?"
>> >> first :f
>> == 
>> >> second :f
>> == [var3: now/time]
>> >> block? :f
>> == false
>> >> series? :f
>> == false
>> So to-string function returns only "?function?" string and
>> although it is possible to apply to function first, second,... it is
>> itself not a block. So is possible to convert function to block and
>> visa versa?
VN> 'mold, to a string. or:
VN> append2: func third :append second :append ; rebuild from the blocks
>> 3. If I store object function as string or some other way, how can I
>> restore it? If I apply "do" to string containing function body, then I
>> will get function. But...it will not be bounded to the context of the
>> object - it will not be able to access instance variables.
>> So how can I dynamically create object function?
VN> a) handler approach
VN> b) convert data to a rebol source-string, then do that.
VN> means wrap "make object!" around etc.
VN> then 'do does the rest.
>> 4. And one more question - is there some kind of weak references in
>> A lot of thanks in advance
>> GI> You may even get a volunteer or two to help you!
>> GI> --Gregg