Page 1 of 1
Getting my current function name
Posted: Fri Jun 30, 2006 10:31 pm
by pjot
Hi,
Suppose I have the following program:
Code: Select all
(define (func)
(println "func")
)
(func 1 2 3)
(exit)
Now, this function prints it's own functionname. However, to me this is quite useless, as I have to hardcode the functionname into the source. And as I am generating functions with names from from sym'd strings, I cannot use this.
One might think it can be solved as follows:
Code: Select all
(define (func)
(println (args 0))
)
(func 4 5 6)
(exit)
However, (args 0) will show the passed value '4' instead of the functionname.
Has any of you a suggestion on how I can retrieve the functionname while I am in that function? So not by hard-coding the name? :-)
Actually I am looking for some kind of "self" or a similar thing as the Java construction "me".
Peter
Posted: Fri Jun 30, 2006 10:48 pm
by newdep
Yes i would like to see a function called 'self
I tried many times to create something inside newlisp
but it has to be done on a lower level in C, as it seems..
(define (some-random-name) (println 'self))
>some-random-name
or
(define (some-random-name) (println (first self)))
>some-random-name
or
(define (some-random-name) (println self))
>(define (some-random-name) (println self))
or perhaps 'lambda? could return the NAME else 'nil ->
(define (some-random-name) (lambda? self))
>some-random-name
Posted: Sun Jul 02, 2006 10:21 am
by pjot
I guess we finally found something which cannot be done with newLISP... ;-)
For my program I found some sort of workaround, but it's ugly. So a function called "SELF" or "ME" would be very handy indeed. Where can I raise this change request? ;-)
Regards
Peter
Posted: Tue Jul 04, 2006 8:28 pm
by newdep
Lutz, Do you have any idea on my question above?
Could it be done with 'dump perhaps?
Regards, Norman
Re: Getting my current function name
Posted: Thu Jun 10, 2010 2:07 am
by may
Bump.
Did anyone ever make any progress on this, at all?
Re: Getting my current function name
Posted: Thu Jun 10, 2010 6:07 am
by pjot
Well I found another way of solving my issue without the need for a 'SELF and never asked further...
Peter
Re: Getting my current function name
Posted: Thu Jun 10, 2010 7:00 am
by may
pjot wrote:Well I found another way of solving my issue without the need for a 'SELF and never asked further...
Peter
Gotcha. Thanks!
Re: Getting my current function name
Posted: Fri Jun 11, 2010 9:45 pm
by xytroxon
I thought I found something that might (should?) work...
newLISP manual: sym
Because the function sym returns the symbol looked up
or created, expressions with sym can be embedded directly
in other expressions that use symbols as arguments.
Code: Select all
(setq $fn "myfuncname")
(context 'test)
(define ((sym $fn 'test) num)
(for (i 1 num)
(println i ":" $fn)))
(context MAIN)
(test:myfuncname 5)
But I get:
ERR: symbol expected in function define : ((sym (setq $fn "myfuncname") 'test) num)
(define-macro etc... also does not work...
-- xytroxon
Re: Getting my current function name
Posted: Fri Jun 11, 2010 11:16 pm
by m i c h a e l
This works:
Code: Select all
(setq $fn "myfuncname")
(context 'test)
(set (sym $fn) (lambda (num)
(for (i 1 num)
(println i ":" $fn)
)
))
(context MAIN)
(test:myfuncname 5)
Since you're defining the function within the
test context,
sym already creates the symbol there.
Hope that helps.
m i c h a e l
Re: Getting my current function name
Posted: Sat Jun 12, 2010 11:25 am
by cormullion
This kind of works:
Code: Select all
(define-macro (define! farg)
(set (farg 0)
(letex (func (farg 0)
arg (rest farg)
arg-p (cons 'list (map (fn (x) (if (list? x) (first x) x))
(rest farg)))
body (cons 'begin (args)))
(lambda
arg (let (_self 'func) body)))))
(define! (f a b c)
(println "I'm " _self)
(+ a b c))
(define! (g a b c)
(println "and I'm " _self)
(+ a b c))
(f 7 8 9)
;-> I'm f
6
(g 10 11 12)
;-> and I'm g
33
Re: Getting my current function name
Posted: Mon Jun 14, 2010 7:08 pm
by xytroxon
Thanks!!!
I decided to use a string based "template function" and use eval-string to dynamically create the desired functions... Each function name being listed once in a list and so is now correctly spelled in the three places it is used in the function... This replaces hand writing dozens of (define (myfunc x) ... )... The module code is now embarrassingly small ;p) Ah the POWER of newLISP!!!
---------
I found most useful how (context) gives the current context name...
Should / could (define) give the current function name???
-- xytroxon