摆脱“对自由变量的引用”字节编译警告 [英] Get rid of "reference to free variable" byte-compilation warnings

查看:140
本文介绍了摆脱“对自由变量的引用”字节编译警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个emacs主要模式,它使用缓冲区局部变量来存储一些状态:

 <$ 
(kill-all-local-variables)
(setq mode-name)
(交互式)
foo)
(setq major-mode'foo-mode)
(set(make-local-variable'foo-state)bar))

foo-change-state()
(setq foo-statebaz))

这个工作非常好,并且具有在任何不使用主要模式的缓冲区中的属性, foo-state 变量没有约束(这在我看来是一件好事,因为它避免了符号表的混乱)。



然而,字节编译这样一段代码会产生以下警告:

 警告:赋值给空闲变量`foo-state'

使用 defvar 摆脱警告,但副作用是 fo o-state 现在是无处不在的,这在我看来是不可取的。



有没有办法摆脱警告,而还没有绑定每个缓冲区中的模式特定变量?或者我认为这些变量不应该被全局声明时,我错了?

解决方案

code>(defvar foo-state)。注意没有第二个参数。还要注意,这样的声明只适用于找到它的文件。


I'm writing an emacs major mode, which uses buffer-local variables to store some state:

(defun foo-mode ()
  "My nice major mode"
  (interactive)
  (kill-all-local-variables)
  (setq mode-name "foo")
  (setq major-mode 'foo-mode)
  (set (make-local-variable 'foo-state) "bar"))

(defun foo-change-state ()
  (setq foo-state "baz"))

This works very well and has the property that in any buffer not using my major mode, the foo-state variable is not bound (which is a good thing in my opinion, since it avoids cluttering the symbols table).

However, byte-compiling such a piece of code produces the following warning:

Warning: assignment to free variable `foo-state'

Using defvar gets rid of the warning, but has the side-effect that foo-state is now bound everywhere, which is undesirable in my opinion.

Is there a way to get rid of the warnings while still not binding the mode-specific variables in every buffer? Or am I mistaken when I think these variables should not be declared globally?

解决方案

The official way to do what you want is (defvar foo-state). Note the absence of a second argument. Note also that such a declaration only applies to the file where it is found.

这篇关于摆脱“对自由变量的引用”字节编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆