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

source fails for object function

 [1/2] from: pat665::ifrance::com at: 10-Dec-2001 21:56


Hi all, I have used 'source to verify that two functions were identical. However this technic fails for object function. This small script shows this. Rebol [] a: func [n [integer!]] [ 2 * n ] b: :a obj: make object! [ myfunc: none ] c: func [n [integer!]] [ 3 * n ] obj/myfunc: :c print obj/myfunc 3 ; works ! source a source b source c source obj/myfunc ; fails ! ** Script Error: source expected word argument of type: word ** Where: do-boot ** Near: source obj/myfunc This leads to several questions : - why does source fail ? - how one can test if two functions are identical ? - is the >> obj/myfunc: :c << a correct way to reference external function ? - is there any other way to do that ? BTW as it is not much obvious the goal is to reference a function in an object. The context is that my object holds the main logic of the program, but I want it independant of the display functions. So display functions are defined externally. However the object must be able to display some stuff too so it has some function "pointers" (if I can say so). Patrick nb : I posted this yesterday, but it seems to have vanished away, because I did not receive it back.

 [2/2] from: greggirwin:mindspring at: 12-Dec-2001 12:22


Hi Patrick, << - why does source fail ? >> Source wants a word. Do "source source" at the console to see how it works. << - how one can test if two functions are identical ? >> You can use the reflective properties of functions to retrieve their internals and compare them. For example, I have a 'lib object, which contains a 'math object, which, in turn contains a 'sign function. The 'in function lets you retrieve the value of a word in a given context. Examples speak louder than words so...
>> fn: get in lib/math 'sign >> type? :fn
== function!
>> first :fn
== [value]
>> second :fn
== [ if positive? value [return 1] if zero? value [return zero] if negative? value [return -1] ]
>> third :fn
== [ {Returns: 1 if value is positive, -1 if value is negative, 0 if value is 0} value [number! money! time!] ]
>> source fn
fn: func [ {Returns: 1 if value is positive, -1 if value is negative, 0 if value is 0} value [number! money! time!] ][ if positive? value [return 1] if zero? value [return zero] if negative? value [return -1] ]
>> print mold get in lib/math 'sign
func [ {Returns: 1 if value is positive, -1 if value is negative, 0 if value is 0} value [number! money! time!] ][ if positive? value [return 1] if zero? value [return zero] if negative? value [return -1] ] The /Core docs talk about the reflective properties of functions, so that would be a good place to look as well. HTH! --Gregg