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

accessing your own function signature / context

 [1/6] from: koopmans::itr::ing::nl at: 24-Oct-2001 9:31


Hi, Is there any way to access the context of a function in the function and thus access its data. f: func [ a /b c ] [ ... ] Now I would like to access first :f from within f so I can inspect it (not change it) Basically, I want 'self for :f Thanks, Maarten

 [2/6] from: matt:fitzgerald:bigpond at: 24-Oct-2001 17:53


Self modifying code!! :-) Well, when you're in a function, aren't you already in its context? Or do you mean access the context of function f in function y? The only way I can see is if you made function f a member/method/function of an object, would you be able to do it then? Hmmm... -Matt

 [3/6] from: petr:krenzelok:trz:cz at: 24-Oct-2001 10:01


->> f: func [a] [print a print mold second :f] ->> f "aaa" aaa [print a print mold second :f] -pekr- Maarten Koopmans wrote:

 [4/6] from: koopmans:itr:ing:nl at: 24-Oct-2001 10:22


f does not now its name its f in my case. --Maarten

 [5/6] from: joel:neely:fedex at: 24-Oct-2001 6:46


Hi, Maarten, Maarten Koopmans wrote:
> f does not now its name its f in my case. > --Maarten
<<quoted lines omitted: 11>>
> > > function and thus access its data. > > >
Is this a function you will write yourself? Two tricks I've experimented with (briefly!) would require additional voodoo and are thus beyond the scope of a simple SELF idea, but for what they may be worth to you... 1) Pass the function as an argument to itself:
>> f: func [a b c me] [
[ c [ print ["My sum is" a + b] [ print ["My other arg was" mold get first second :me] [ ]
>> f 1 2 "Hi!" :f
My sum is 3 My other arg was "Hi!" 2) Create the function in a context that retains a reference to the function itself:
>> f: use [myself] [myself: func [a b c] [
[ c [ print ["My sum is" a + b] [ print ["My other arg was" mold get first second :myself] [ ] [ ]
>> f 2 3 "Bye!"
My sum is 5 My other arg was "Bye!" The second option above requires copying if you want to re-use the body block, of course... -jn- -- ; sub REBOL {}; sub head ($) {@_[0]} REBOL [] # despam: func [e] [replace replace/all e ":" "." "#" "@"] ; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"} print head reverse despam "moc:xedef#yleen:leoj" ;

 [6/6] from: koopmans:itr:ing:nl at: 24-Oct-2001 14:00


Hi Joel, This should do the trick in my case. In Rugby I'll generate new kind of stub code that needs to access its own signature. What you told me is definitely the way to go. Good to feel stupid again ;-) --Maarten

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted