Code Red > AutoLISP (Vanilla / Visual)

local variable used as function?

(1/2) > >>

daron:
Here is a piece of code I've got written that works fine,...

--- Code: ---;;;---------------------------------------------------------------------;
;;; variant item ;
;;;---------------------------------------------------------------------;
(defun vl-variant (method obj)
     (vlax-safearray->list
 (vlax-variant-value
      (method obj)
 )
     )
)
--- End code ---

...but I was checking the file and got this

--- Quote ---;warning: local variable used as function: METHOD
--- End quote ---

I inspected METHOD and got nil, so what's making it local? Here's the calling function.

--- Code: ---(vl-variant vla-GetCanonicalMediaNames LayoutObj)
--- End code ---

LayoutObj is ActiveLayout property.
I use that as the list in a foreach statement.

SMadsen:
Don't know why it says 'local variable' instead of argument or parameter. Arguments are just local variables that are bound upon entry but it should state the difference. I guess it just has a limited vocabulary?

Anyway. The interpreter have no way of knowing that method will be given as a function. It sees that it is being used as a function and finds it suspicious.
It runs because it works. When you set a breakpoint and trace the stack you will see a call like (#<SUBR @020333e8 vla-GetCanonicalMediaNames> #<VLA-OBJECT IAcadLayout 01af0704>), which is legal.

However, if you were to compile the routine with linking on it would not be able to link to what it sees as a function (added: it is not defined anywhere as a function and therefore can't pass an address to the compiler):

; warning:   cannot LINK METHOD
;               The function is undefined

To get past such problems, you should evaluate it with EVAL:

(eval (list method obj))

daron:
Funny, my biggest concern is when compiling code into fas files. As far as the eval thing goes, are you saying to put (eval (list method obj)) in the arguments cue or where?

SMadsen:

--- Code: ---(defun vl-variant (method obj)
     (vlax-safearray->list
     (vlax-variant-value
          (eval (list method obj))
     )
     )
)
--- End code ---


And preferrably call the function, or method, quoted in order to be real safe:

(vl-variant 'vla-GetCanonicalMediaNames LayoutObj)

Kerry:

--- Quote from: SMadsen --- < snip >
And preferrably call the function, or method, quoted in order to be real safe:
(vl-variant 'vla-GetCanonicalMediaNames LayoutObj)
--- End quote ---

Thats the way I do it too Daron/Stig, and haven't had a problem to date.
The extent of error trapping you want to do will depend on your personality .... etc

However .. I would not prefix the function name with vl- , as it is not a VisualLisp built in function, and could get confusing for someone viewing your source.
It sure reads better than this sort of thing scattered dozens of times in your code,

--- Code: ---
  (setq PrinterList (vlax-safearray->list
                      (vlax-variant-value (vla-getplotdevicenames LayoutObj))
                    )
  )

--- End code ---


Regards
Kerry

Navigation

[0] Message Index

[#] Next page

Go to full version