Getting my current function name

Q&A's, tips, howto's
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Getting my current function name

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post 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
-- (define? (Cornflakes))

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post 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

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Lutz, Do you have any idea on my question above?
Could it be done with 'dump perhaps?

Regards, Norman
-- (define? (Cornflakes))

may
Posts: 2
Joined: Thu May 06, 2010 11:04 pm
Location: Iowa City, Iowa
Contact:

Re: Getting my current function name

Post by may »

Bump.

Did anyone ever make any progress on this, at all?

pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Re: Getting my current function name

Post by pjot »

Well I found another way of solving my issue without the need for a 'SELF and never asked further...

Peter

may
Posts: 2
Joined: Thu May 06, 2010 11:04 pm
Location: Iowa City, Iowa
Contact:

Re: Getting my current function name

Post 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!

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Getting my current function name

Post 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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Re: Getting my current function name

Post 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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Getting my current function name

Post 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

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Getting my current function name

Post 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
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

Locked