Need help understanding a piece of code

Discussion in 'AutoCAD' started by Larry Travis, Jun 23, 2004.

  1. Larry Travis

    Larry Travis Guest

    Hello. I am trying improve my programming techniques, but am having a
    problem understanding a code snippet provided in the AutoCAD help. I have
    two questions regarding the code below. First, why is that cond function
    required there? The variable has already ben ititialized and set to nil, so
    why test it (assuming that is what the cond is doing)? Second, how does the
    cond function work here anyway? What is the deal with the (*acad-object")?
    If you just throw a variable into a list you get will get an error won't
    you? I just can't make sense of this.


    (setq *acad-object* nil) ; Initialize global variable
    (defun acad-object ()
    (cond (*acad-object*) ; Return the cached object
    (t
    (setq *acad-object* (vlax-get-acad-object))
    )
    )
    )
     
    Larry Travis, Jun 23, 2004
    #1
  2. Larry Travis

    Don Butler Guest

    *acad-object* is set to NIL at loadtime.

    Subsequent calls are to the "ACAD-OBJECT" function. It is designed to run
    and set *acad-object* to (vlax-get-acad-object) only once.

    Don
     
    Don Butler, Jun 23, 2004
    #2
  3. Larry Travis

    Tom Smith Guest

    As far as I can tell, you could replace the whole mess with a simple (setq
    *acad-object* (vlax-get-acad-object)). It doesn't matter whether the
    variable has been set previously or not, that will still work. And I agree
    it seems nonsensical to null it out and then test for it.

    The cond works as intended though. Each list in a cond construction has a
    "test" expression and (optionally) one or more "result" expressions which
    are evaluated if the test expression evaluates non-nil. As usal with lisp,
    the last thing evaluated is returned. If there are no "result" expressions
    in a cond list, the value of the "test" expression -- the last thing
    evaluated -- is returned.

    So (*acad-object*) simply means, if *acad-object* has a value, return it. An
    unnecessarily verbose version of the same thing would be:

    (cond
    ((not (null *acad-object*) )
    *acad-object*
    )
    (t
    (setq *acad-object* (vlax-get-acad-object))
    )
    )
     
    Tom Smith, Jun 23, 2004
    #3
  4. Larry Travis

    Don Butler Guest

    It also might help to analyze the return values...

    <CLIP>
    Command: (setq *acad-object* nil)
    nil

    Command: (defun acad-object ()
    (_> (cond (*acad-object*)
    ((_> (t
    (((_> (setq *acad-object* (vlax-get-acad-object))
    (((_> )
    ((_> )
    (_> )
    ACAD-OBJECT

    Command: (acad-object)
    #<VLA-OBJECT IAcadApplication 00b9b5e4>

    Command: (acad-object)
    #<VLA-OBJECT IAcadApplication 00b9b5e4>

    <END CLIP>

    Don
     
    Don Butler, Jun 23, 2004
    #4
  5. Larry,

    The first statement:
    (setq *acad-object* nil)

    runs only when the .lsp file that contains the statement is loaded. So the
    global variable will be set to nil only upon the load.

    Now, when the function (acad-object) is called, it will ultimately return
    the Acad Application object. The (cond) is used to test if the global
    variable has been bound.

    The first time the function is called, the global variable is nil, so the
    cond skips the first test:
    (*acad-object*)
    and evaluates the 2nd test, which is T, so it executes:
    (setq *acad-object* (vlax-get-acad-object))
    which, of course, returns the Acad Application object

    This binds the global variable to the Acad Application object. Now, the
    _next_ time the function is called, without being reloaded, the global
    variable _does_ evaluate to something (the Acad Application object), so the
    cond's first test is used as the return value of the function itself.

    --
    R. Robert Bell


    Hello. I am trying improve my programming techniques, but am having a
    problem understanding a code snippet provided in the AutoCAD help. I have
    two questions regarding the code below. First, why is that cond function
    required there? The variable has already ben ititialized and set to nil, so
    why test it (assuming that is what the cond is doing)? Second, how does the
    cond function work here anyway? What is the deal with the (*acad-object")?
    If you just throw a variable into a list you get will get an error won't
    you? I just can't make sense of this.


    (setq *acad-object* nil) ; Initialize global variable
    (defun acad-object ()
    (cond (*acad-object*) ; Return the cached object
    (t
    (setq *acad-object* (vlax-get-acad-object))
    )
    )
    )
     
    R. Robert Bell, Jun 23, 2004
    #5
  6. Larry Travis

    Larry Travis Guest

    Thanks so much to all of you for taking the time to analyse the code and
    explain it to me. I had forgotten that the first line only executes upon
    loading, so that makes sense now. Also, I had previously thought a cond
    test always had to have "result" expression following the test expression.
    Now realizing otherwise, this cond example makes sense.

    Thanks again, guys. I have learned something valuable today.

    LT
     
    Larry Travis, Jun 23, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.