0

I need a function with DEEP parameter that if it is placed in the body of another function returns the list of the arguments of the ancestor function depending on DEEP parameter.

If DEEP = 0 returns list arguments of its parent function

IF DEEP = 1 returns list arguments of grand parent function and so on

(defun ask-fun (DEEP)
     BODY)

BODY is unknown, must be determined.

For example:

(defun parent-fun (a b c)
    .......
    (ask-fun 0) 
    .......)

(ask-fun 0) to returns (a b c) the argument list of its parent.

(defun grand-parent-fun (x y z)
     .....
     (parent-fun a b c)
     ......)

calling grand-parent-fun function

(grand-parent-fun x y z)

then (ask-fun 1) called from body of parent-fun function to return the list arguments of grand-parent-fun respectively (x y z).

I need that function to create another functions X that do others things in body of parent function of X. For example:

(defun get-arg-sum-fun ()
    (apply '+ (ask-fun 1)))

returns the sum of the arguments of parent function of 'get-arg-sum-fun if the arguments are only numbers.

It is an flexible instrument to create a lot of useful functions to calculate expression with arguments of ancestor functions.

Drew
  • 77,472
  • 10
  • 114
  • 243

3 Answers3

0

Instead, just use &rest to obtain a list of the function arguments.

(defun get-arg-sum-fun (&rest args)
  (apply '+ args))
phils
  • 50,977
  • 3
  • 79
  • 122
0

There’s no way to write such a function. This sounds like an X–Y problem; you might be better off asking about what you wanted to use such a function for.

db48x
  • 17,977
  • 1
  • 22
  • 28
0

I'm not recommending that you use this, and honestly have no idea whether it's reliable/safe outside of debugging scenarios, but this is the only way that I know of to do what you're asking for.

(defun grand-parent-fun (x y z)
  (parent-fun x y z))

(defun parent-fun (a b c) (ask-fun 0))

(defun ask-fun (deep) (require 'backtrace) (let ((frames (backtrace-get-frames))) (setq frames (nthcdr 3 frames)) (while (not (backtrace-frame-evald (car frames))) (pop frames)) (backtrace-frame-args (nth deep frames))))

It is a flexible instrument to create a lot of useful functions to calculate expression with arguments of ancestor functions.

That doesn't sound like debugging.

If it's just for your own personal use then anything goes of course; but if you have any thoughts of sharing such code with others than this probably isn't a sensible idea.

phils
  • 50,977
  • 3
  • 79
  • 122
  • It’s better to pretend that there’s no way to do it; for one thing this constructs a whole list of frames to get one value out of it, and for another it retrieves and copies every single argument list passed to every single function in the backtrace, retrieves one of them, and then throws the rest away. – db48x Mar 11 '24 at 21:06