Getting my current function name

Started by pjot, June 30, 2006, 03:31:09 PM

Previous topic - Next topic

pjot

Hi,



Suppose I have the following program:



(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:



(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

#1
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

#2
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

#3
Lutz, Do you have any idea on my question above?

Could it be done with 'dump perhaps?



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

may

#4
Bump.



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

pjot

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



Peter

may

#6
Quote from: "pjot"Well I found another way of solving my issue without the need for a 'SELF and never asked further...



Peter


Gotcha.  Thanks!

xytroxon

#7
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.



(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

#8
This works:


(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

#9
This kind of works:


(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

#10
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