3

You can read this on AutoCAD knowledge website:
"Note: You can define multiple user functions with the same name, but have each definition accept a different number or type of arguments."
Has anybody using this feature? I tried but does not work at all. I can call only the latest defined function.If I call like this (file::AppendFile arg1) then autocad said I give too less argument
enter image description here

Zoon81
  • 66
  • 6
  • does `::` have some special meaning as a part of a symbol name in Autolisp, or is it just a convention you're using? – Will Ness Jan 03 '18 at 11:26

2 Answers2

3

I'm not at a computer with AutoCAD installed, so I can't check if AutoLISP works the way the documentation says it should, but I do know I've seen a workaround to pass a variable number of arguments into a function.

The trick is to pass all your arguments as a single list, and then process that list in the body of the function. For example:

(defun myFunction (argsList / path header)
  (setq path (car argsList))
  (setq header (cadr argsList))
  (someFunction path "a" header)
)

... and then you'd call your function with (myFunction '("arg1")) or with (myFunction '("arg1" "arg2")).

Note that in my examples above I'm using the list constructor literal, so it will pass in the actual strings "arg1" and "arg2". If you want to pass in the contents of variables, you'd need to use the form (myFunction (list var1 var2)) instead, because (myfunction '(var1 var2)) would pass in the symbols 'var1 and 'var2 instead of their values.

It's a little bit ugly, but it's an option.

Dan Getz
  • 8,774
  • 6
  • 30
  • 64
Wes Lord
  • 463
  • 5
  • 11
2

"Note: You can define multiple user functions with the same name, but have each definition accept a different number or type of arguments."

This is not possible in AutoLISP: the last defun expression evaluated will overwrite all previous definitions of the symbol in the namespace - hence, in your example the file:AppendFile function would require two arguments, as the second defun expression will immediately redefine the function.

The only way to supply two arguments (other than supplying a list of arguments of varying length) would be to evaluate the file:AppendFile function prior to the evaluation of the second defun expression.

Lee Mac
  • 15,615
  • 6
  • 32
  • 80