[REBOL] Re: Storing/loading object functions
From: nitsch-lists:netcologne at: 6-Dec-2003 22:10
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
You want to store the class for the object or indivuidual functions?
> But in Rebol there is no class. And looks like there is no way to find
> out prototype object for the particular object instance.
except you store the classname in the object
a-class: make object! [type: "a-class" a: none]
a more efficient way is to use a method-object, like faces do.
then you have
a-handler: context [
add: func [this increment] [this/value: this/value + increment]
; ^ note we pass 'this explicitely
a-class: context [handler: a-handler a: none]
a-object: make a-class [value: 5]
a-object/handler/add a-object 3
the call-syntax is not very handy,
but complete /view-styles are based on this system.
Usually there is a dispatch-function, so that one can write
ao-add: func[this incr][this/handler/add this 3]
ao-add a-object 4
see 'do-face in /view,
or 'show, which dispatches to face/feel/redraw and does some more work.
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.
str probe mold :append
append2: do str
; the ":" is important here!
; binding may make problems. Keep the functions better outside
; the data-object. see handler-approach.
(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?
'mold, to a string. or:
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?
a) handler approach
b) convert data to a rebol source-string, then do that.
means wrap "make object!" around etc.
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