Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Howto declare optional parameters for defun ?

16 REPLIES 16
Reply
Message 1 of 17
yC_tedium
2853 Views, 16 Replies

Howto declare optional parameters for defun ?

Anyone know howto declare optional parameters for defun ?
16 REPLIES 16
Message 2 of 17
devitg
in reply to: yC_tedium

Like what??


Message 3 of 17
Anonymous
in reply to: yC_tedium

Options arguments? You really can't in LISP. You can pass
the arguments as a list.


wrote in message news:6100571@discussion.autodesk.com...
Anyone know howto declare optional parameters for defun ?
Message 4 of 17
Anonymous
in reply to: yC_tedium

Assuming you mean optional arguments, the only way I know of is use a list as the
argument. Like this.

(defun test (arglst / var var)
(if firstarg...

Joe Burke
Message 5 of 17
Anonymous
in reply to: yC_tedium

Jason Piercey wrote:
> Options arguments? You really can't in LISP. You can pass
> the arguments as a list.
>
>
> wrote in message news:6100571@discussion.autodesk.com...
> Anyone know howto declare optional parameters for defun ?


In Common Lisp it would be

(defun somefun (a b &optional c d) ...)

but AutoLISP/VisualLisp doesn't allow this usage for end-users.
Autodesk can do something like that in the compiler, so some built-in
functions can have optional arguments, but no way for the ordinary user
to do this.

So, pack the variable stuff in a list and dismantle it as needed inside
your defun.

--
Message 6 of 17
devitg
in reply to: yC_tedium

As Tediun , did not appear any more at the DG .

I make this guess



(defun volumen-cal ( heigth wide length )



(if (not heigth )

(setq heigth 100)

)



(if (not wide)

(setq wide 50)

)



(if (not length)

(setq length 100)

)



( * heigth wide length)



)





Then





(setq volumen (volumen-cal 25 nil 765))



Any way , the NIL for any optional argument SHALL be send














Message 7 of 17
Anonymous
in reply to: yC_tedium

DEVITG,

Given your example there are no optional arguments.

Joe Burke
Message 8 of 17
devitg
in reply to: yC_tedium

Ok , but it seem to be more a semantic fact , tha a progarmming fact.



Each argumen sent as nil , could be a option.


Message 9 of 17
yC_tedium
in reply to: yC_tedium

Martti,
Can't do it ?... Can I use any kind of compiler to do it also ?_?

Jason,
Yes, "arguments"

DEVITG,
Like those AutoCAD can do.
It was coz I searching in the help files, and saw those many functions form autocad allowing optional arguments, so I try asking how can I...
Message 10 of 17
yC_tedium
in reply to: yC_tedium

;Yeah, that's not optional =_="

;error: too few arguments

;but thanks anyway.
(yC)
Message 11 of 17
Anonymous
in reply to: yC_tedium


It's frustrating, but you can not do it, period.
AutoLISP is a simple low level API and it's an intrerpreted language, so there
is no compilation involved. As for the functions that you see in the help files,
they are not defined with AutoLISP and DEFUN, this would be kind of an
endless loop, isn't it, because all this AutoLISP language should be implemented
somewhere somehow, with some other language, so these are the native
AutoLISP functions, defined by the Autodesk programmers in C++, which gives
the the posibility to build functions with optional parameters.

 

Regards


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Martti,
Can't do it ?... Can I use any kind of compiler to do it also ?_? Jason, Yes,
"arguments" DEVITG, Like those AutoCAD can do. It was coz I searching in the
help files, and saw those many functions form autocad allowing optional
arguments, so I try asking how can I...
Message 12 of 17
devitg
in reply to: yC_tedium

Any way , option or no option.



Could you, please, tell us what do you mean, or need, when ask for an OPTION?


Message 13 of 17
Anonymous
in reply to: yC_tedium

I think the point you are missing is the fact the function in question is designed to
accept a list argument. So it does not error as long as a list is passed. What
happens afterwards is up to the function.

Joe Burke
Message 14 of 17
stevor
in reply to: yC_tedium

My guess is that the error is from some call from within your function, not the lack of an adequate initial argument list.

In Autolisp, the variable argument list is passed by a list. If the argument exists and is evaluated to nil, it is test as a list by the TYPE function, although contains nothing.

If your function requires some values from within the list, you simply test for each expected argument within the list and in not provided from the list, then provide default values by global variables or internally derived values. One would keep track of the assignments by the order of the expected arguments, or the types of the elements within the list.
.
S
Message 15 of 17
Anonymous
in reply to: yC_tedium

Some Buddy wrote:
> It's frustrating, but you can not do it, period. AutoLISP is a simple
> low level API and it's an intrerpreted language, so there is no
> compilation involved. As for the functions that you see in the help
> files, they are not defined with AutoLISP and DEFUN, this would be kind
> of an endless loop, isn't it, because all this AutoLISP language should
> be implemented somewhere somehow, with some other language, so these are
> the native AutoLISP functions, defined by the Autodesk programmers in
> C++, which gives the the posibility to build functions with optional
> parameters.
>
> Regards
>
> a écrit dans le message de news:
> 6101745@discussion.autodesk.com
> <6101745>...
> Martti, Can't do it ?... Can I use any kind of compiler to do it
> also ?_? Jason, Yes, "arguments" DEVITG, Like those AutoCAD can do.
> It was coz I searching in the help files, and saw those many
> functions form autocad allowing optional arguments, so I try asking
> how can I...

Actually, that has nothing to do regarding compilation or
interpretation, neither necessarily about the implementation language:
most Common Lisp compilers are written in Common Lisp, with only very
few low-level constructs written in C.

Some error reports and debugging output give hints that the internals of
the Visual Lisp system have more Common Lisp -like constructs available
than what are shown to the end user.

Anyway, the only way I know for you to be able to define optional
parameters in AutoLISP/Visual Lisp would be to get yourself hired by
Autodesk as a lisp implementor.

--
Message 16 of 17
Anonymous
in reply to: Anonymous

Resurrecting an old thread here...

 

 

So, I was thinking of passing a block name to a function to insert the block into a drawing.  Then an "optional" boolean argument to whether or not it should be scaled according to say LTS or Dimscale (or else just use scale factor of 1).  To be used in a menu.

 

So, according to this thread I would need to define the function w/ 1 arglist variable like:

 

(Defun InsertBlock (arglist /...)

 

and my arglist could look somethign like:

'("Blockname" T) or just '(Blockname)

 

Then

(if (nth 1 arglist)

  (setq blockscale (getvar "ltscale"))

  (setq blockscale 1)

  );if

 

Seems like too much work and maybe I should just always call the function w/ (insertblock "blockname" t) or (insertblock "blockname" nil) every time.

 

Anyone else have suggestions?

like.. "Use toolpalettes" perhaps.. haha.

Message 17 of 17
dgorsman
in reply to: Anonymous

Not the best of examples.  Small numbers of arguments and boolean yes/no arguments are almost always better handled with dedicated arguments e.g. (insertABlock "Name" T) and (insertABlock "Name" nil), or pass the desired scale value as a real.  If you felt the burning desire to add more code you could create wrapper functions to call a central function with the extra arguments e.g. (insertAnLTSBlock ...) would call (insertABlock "Name" (getvar "LTSCALE")).

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost