about "assignment to free variable"

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

about "assignment to free variable"

Michelangelo Rodriguez
Hi All,
When i compile a piece of code the byte-compiler reports a warning
telling me that i'm trying to assign to a free variable:
But the variable in question is, atleast i think, referred to a local
variable because it is an argument of the function.
So, what it means exactly?
The code works fine even if compiled.
If it is necessary i can post the code, even if it is large.
Thanks for your replies and best regards.
Michelangelo

Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Stefan Monnier
> When i compile a piece of code the byte-compiler reports a warning
> telling me that i'm trying to assign to a free variable:
> But the variable in question is, atleast i think, referred to a local
> variable because it is an argument of the function.

I think your "at least I think" is in error.  But you'd have to show me
the relevant code.

"*free* variable" doesn't mean "a variable which will not exist when this
code is run" but "a variable which is not visible in the surrounding scope".

> The code works fine even if compiled.

Until it doesn't.

> If it is necessary i can post the code, even if it is large.

Pointing me to the relevant file+linenb should be sufficient.


        Stefan


Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Stefan Monnier
>> When i compile a piece of code the byte-compiler reports a warning
>> telling me that i'm trying to assign to a free variable:
>> But the variable in question is, atleast i think, referred to a local
>> variable because it is an argument of the function.
> I think your "at least I think" is in error.  But you'd have to show me
> the relevant code.

Looking at the current greader.el in your `master` branch, I suspect
you're talking about the warning

    In greader-change-backend:
    greader.el:203:18:Warning: assignment to free variable ‘backend’

where the problem is that this use of `backend` is within the
interactive spec: while the interactive spec is textually within the
definition of the function, it is code that's run outside of the
function (it's executed in order to build the list of arguments needed
to call the function).

So maybe the patch below would be the fix to this warning.


        Stefan


diff --git a/greader.el b/greader.el
index b854e0fa9..9491ac521 100644
--- a/greader.el
+++ b/greader.el
@@ -195,11 +195,12 @@ For example, if you specify a function that gets a sentence, you should specify
     (funcall greader-actual-backend command)))
 
 (defun greader-change-backend (&optional backend)
-  "changes back-end. if backend is specified, it changes to backend, else it cycles throwgh available back-ends."
+  "Change back-end.
+If BACKEND is specified, change to it, else cycle through available back-ends."
   (interactive
    (list
     (if current-prefix-arg
- (setq backend (read-from-minibuffer "backend: ")))))
+ (read-from-minibuffer "backend: "))))
   (if (functionp backend)
       (if (memq backend greader-backends)
   (setq greader-actual-backend backend)


Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Michelangelo Rodriguez
In reply to this post by Stefan Monnier
Hi Stefan,
The file is greader.el, fetched from gitlab.
The warning is at line 203, at least the line reported by the compiler.
Thanks in advance.

Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Michael Heerdegen
Michelangelo Rodriguez <[hidden email]> writes:

> Hi Stefan, The file is greader.el, fetched from gitlab.  The warning
> is at line 203, at least the line reported by the compiler.  Thanks in
> advance.

Yes, it's as Stefan told you some minutes ago.

I also see these warnings:

| greader.el:182:10:Warning: reference to free variable `greader'

Seems there's a quote missing.

| greader.el:235:12:Warning: reference to free variable `dtk-quiet'

That variable should probably be declared.

...etc.  But I guess you can cope with the other warnings?


Michael.

Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Michelangelo Rodriguez
In reply to this post by Stefan Monnier
Ok, thanks! so, it was not necessary to assign the return value of
read-from-minibuffer to backend, because it assigns already the first
element.
wright?

Reply | Threaded
Open this post in threaded view
|

Re: about "assignment to free variable"

Stefan Monnier
> Ok, thanks! so, it was not necessary to assign the return value of
> read-from-minibuffer to backend, because it assigns already the first
> element.
> wright?

More or less.  It doesn't really "assign" it.  Instead it takes the list
constructed by the EXP in (interactive EXP) and calls the function with
it, as if you had written (apply FUNCTION EXP).


        Stefan