Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: Hack

From: rotenca:telvia:it at: 29-Dec-2001 2:03

Hi Sunanda,
> > Only the third time the hack fails. > > I think you mean the 2nd fails, the first and last work.
No, the third: Hacko: make Object! [ hackF: func ["hackF" /local myself] [ error? probe err: disarm try [2 / 0] myself: get in err 'where print [ "my name is:" myself "^/my body is:" mold second get myself "^/my spec is:" mold third get myself ] ] hackf ; <- 1 from inside the object : works ] do in hacko 'hackf ; <- 2 : works hacko/hackf ; <- 3 : fails do bind [hacko/hackf] in hacko 'self ;<- 4 : works (if the program is not stopped by previous error) And works also if the function is called with a word local to a function or to a Use block. The only failure case is the path, but if one does not know how the function is called, he can't be sure about what the word points to. If we know that the function is defined in an object we could try to bind the word to the object context, but we must know at least a word of the context or be sure that 'self has not been redefined.
> On a related subject, I'd like a way to get the name of an object. Example: > > MyObject: make object! [a: 0 b: 0 c: 0] > MyFunc MyObject > ... > MyFunc: func [obj [Object!]][ > > if error? [obj/a] [ > Print [?? "is missing its 'a' variable"] > ] ; if > ] ;func > > where we replace the "??" with the magic hack to dereference obj into > "MyObject".
If you write the objects, you could do something like: make-ob-name: func [word [word!] spec [block!]][ set word make object! compose [(spec) self-name: (bind reduce [to-lit-word word] word)] ] make-ob-name 'foo [a: 2] probe foo probe foo/self-name probe get foo/self-name where self-name contains a word (eventually local) which is a pointer to the object. It what do RT with some Vid faces.
> Now I know I could sort of do it like this: > > MyObject: make object! [a: 0 b: 0 c: 0] > MyFunc "MyObject" > > ... > MyFunc: func [obj-String [String!] /local obj][ > > obj: get to-word obj-string > if error? try [obj/a] [ > Print [Obj-string "is missing its 'a' variable"] > ] ; if > ] ;func > > But that feels like a workaround to me, and for precise functional > equivalence, I need to add in the check that Obj is an object.
I'd not use string, because they works only with object defined in the global context (to-word creates always a global word). My try: MyFunc: func [word [word!] /local obj][ obj: get word if all [object? obj error? try [obj/a]] [ Print [mold word "is missing its 'a variable"] ] ; if ] ;func But you should check the error type to be sure. However to know if a word is defined in an object you can do a more simple: if none? in obj 'a [print ["'a is not defined in" mold 'obj]] Ciao Romano