Author Topic: local variable used as function?  (Read 7627 times)

0 Members and 1 Guest are viewing this topic.

daron

  • Guest
local variable used as function?
« on: October 30, 2003, 05:23:47 PM »
Here is a piece of code I've got written that works fine,...
Code: [Select]
;;;---------------------------------------------------------------------;
;;; variant item ;
;;;---------------------------------------------------------------------;
(defun vl-variant (method obj)
     (vlax-safearray->list
 (vlax-variant-value
      (method obj)
 )
     )
)

...but I was checking the file and got this
Quote
;warning: local variable used as function: METHOD

I inspected METHOD and got nil, so what's making it local? Here's the calling function.
Code: [Select]
(vl-variant vla-GetCanonicalMediaNames LayoutObj)
LayoutObj is ActiveLayout property.
I use that as the list in a foreach statement.

SMadsen

  • Guest
local variable used as function?
« Reply #1 on: October 30, 2003, 06:15:19 PM »
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

  • Guest
local variable used as function?
« Reply #2 on: October 30, 2003, 06:23:26 PM »
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

  • Guest
local variable used as function?
« Reply #3 on: October 30, 2003, 06:32:26 PM »
Code: [Select]
(defun vl-variant (method obj)
     (vlax-safearray->list
     (vlax-variant-value
          (eval (list method obj))
     )
     )
)


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

(vl-variant 'vla-GetCanonicalMediaNames LayoutObj)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
local variable used as function?
« Reply #4 on: October 30, 2003, 07:41:15 PM »
Quote from: SMadsen
< snip >
And preferrably call the function, or method, quoted in order to be real safe:
(vl-variant 'vla-GetCanonicalMediaNames LayoutObj)

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: [Select]

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


Regards
Kerry
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.

daron

  • Guest
local variable used as function?
« Reply #5 on: October 30, 2003, 11:46:56 PM »
So, instead of vl-, maybe I should name them dr- to use my initials and remind me that it's a user defined function? That's a good idea Kerry. Thanks.