Author Topic: Optional argument?  (Read 4895 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Optional argument?
« on: April 08, 2004, 10:17:30 AM »
Is there a way in lisp to have an optional argument?

Like
Code: [Select]
(substr string start [length])
where 'length' is optional.
Is there a way to have
Code: [Select]
(defun MyRoutine (arg1 arg2 [arg_Optional] )

Just curious 8)

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

hendie

  • Guest
Optional argument?
« Reply #1 on: April 08, 2004, 10:49:20 AM »
well, the acad help states
Quote
Unlike many of the standard AutoLISP functions, user-defined functions cannot have optional arguments. When you call a user-defined function that accepts arguments, you must provide values for all the arguments.


but could you have a workaround by using a "getkword" or even an "if" staement ?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Optional argument?
« Reply #2 on: April 08, 2004, 11:19:05 AM »
Well my work around was to use a list of arguments.
Something like this.

Code: [Select]
(setq ret_var (MyRoutine (list arg1 arg2 arg_optional)))


(defun myroutine (list_arg)
  (if list_arg
    (progn
      (setq arg1 (car list_arg)
            arg2 (cadr list_arg)
      )
      (if (> (length list_arg) 2)
        (setq arg_optional (caddr list_arg))
        (setq arg_optional nil) ; missing argument default
      )
    )
  )
  ;; more code here
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
Optional argument?
« Reply #3 on: April 09, 2004, 11:57:18 AM »
I've pondered this very idea and even asked questions about it. VBA does let you do optional arguments, but I was thinking of a wordaround for it too. Maybe adding a setq within a defun precursory to the main function that sets all required arguments to nil or 0 before the main function is set. This way there is a value, even if you don't supply one.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Optional argument?
« Reply #4 on: April 09, 2004, 12:14:22 PM »
Not sure if I follow.
I added a line in the code for "missing argument default"
I that what you had in mind?

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10604
Optional argument?
« Reply #5 on: April 09, 2004, 05:53:04 PM »
No, AutoLisp does not have that feature. (Usualy its a symbol procedding an argument in other languages -i.e. (defun MyProcedure (Arg%)...  and it will take as many arg's you want.  These are usualy procedures that are preforming a comon process that can accept multiple args. -i.e. (defun add (arg%) (+ arg%)) but in AutoLisp if you need to preform this on bunches of stuff you need to do it manualy or with an apply or another list handler.

I have wanted to do this before too and i tried to find a good work arround. i couldnt find one that i was happy with.  The 'list' idea youve employed is about the best i could think of then too.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Optional argument?
« Reply #6 on: April 09, 2004, 07:35:39 PM »
Thanks for the input....

CAB
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Optional argument?
« Reply #7 on: April 09, 2004, 09:11:42 PM »
This is some stuff from my library that allows for default values if nil is passed in the parameter list. Note that the quantum of parameters must still be equal.
eg
Code: [Select]

(setq tmpPT (kpsl_getpoint nil nil nil nil nil))

or use an override to suit
Code: [Select]

(setq tmppt (kpsl_getpoint "Pick Work Point"
                           (if pt03 pt03 (setq pto3 (list 0 0 0)))
                           (+ 1 8 32)
                           nil
                           pt03
            )
)

Code: [Select]

;;; kwb 20021103 ---------------------------------------------
;;;
;;; Arguments:
;;; msg : The prompt string.
;;; def : Value to return if response is <enter>.
;;; bit   : initget bit
;;; kwd : Initget keywords string.
;;; bpt  : Base point
;;;
;;; Note : Arguments may be set to nil
;;; requires kpsl_ptos

(defun kpsl_getpoint (msg def bit kwd bpt / returnvalue)
  (or kwd (setq kwd ""))
  (or bit (setq bit 1))
  (setq msg (strcat "\n"
                    (cond (msg)
                          ("Specify Point")
                    )
            )
  )
  (if def
    (setq msg (strcat "\n" msg " <<" (kpsl_ptos def nil nil) ">>: ")
          bit (logand bit (~ 1))
          ; drop the 1 bit if def used
    )
    (setq msg (strcat "\n" msg ": "))
  )
  (initget bit kwd)
  (setq returnvalue (if bpt
                      (getpoint msg bpt)
                      (getpoint msg)
                    )
  )
  (if returnvalue
    returnvalue
    def
  )
)
;;;--------------------------------------------------------
;;;  Convert Angles from DEGREES to RADIANS

(defun kpsl_dtr (ang) (* pi (/ ang 180.0)))


;;; Convert Angles from RADIANS to DEGREES

(defun kpsl_rtd (ang) (/ (* ang 180.0) pi))

;;; kwb 20021103 ------------------------------------------------------
;;; kpsl_ptos (pt xmode xprec

;; Return a point formatted as a string
;; Arguments :
;; pt       : point list
;; xmode  : Units to use , can be nil
;; xprec   : display precision to use , can be nil

(defun kpsl_ptos (pt xmode xprec)
  (or xmode (setq xmode (getvar "LUNITS")))
  (or xprec (setq xprec (getvar "LUPREC")))
  (if pt
    (strcat (rtos (car pt) xmode xprec)
            ","
            (rtos (cadr pt) xmode xprec)
            ","
            (rtos (caddr pt) xmode xprec)
    )
  )
)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.