Pergunta

I am not sure I understand the property type in defcustom. I can perfectly define the following variables without conforming to the type. what is the purpose of type in this context?

(defcustom foo 1 "foo" :type 'string)
(defcustom spam "a" "foo" :type 'integer)
Foi útil?

Solução

First of all, the type for integer is integer, not int, so your specific example will not work with Customize.

The :type determines the edit control used, and provides completion and type checking when saving customization:

Edit control

M-x customize-variable picks an edit control which is best suited to enter a value for the expected type. For instance, a boolean type becomes a toggle button, simple types as integer or string get a line edit, choice becomes a value menu, set a list of checkboxes, etc.

Completion

Within certain edit controls, Customize provides completion. For instance, when you press M-Tab in a line edit for a function type (as used for hook variables), Customize automatically completes function names. If there is more than one matching name, you'll see a pop up buffer with all completion candidates. Likewise, a color widget provides you with completion for all known color names.

Type checking

For all edit controls, Customize checks the type of the current value before saving, and refuses to apply any customization with a non-matching type.

For instance, if you enter a non-numeric into an integer widget, and try to apply or save the customization, Customize will refuse to do so and signal a “This field should contain an integer” error.

Outras dicas

As the Elisp manual (node Variable Definitions) says: It specifies which values are legitimate, and how to display the value."

This affects "appropriate edit control for the user to customize with", as @amalloy says. It affects also whether Customize shows the current value as valid for the :type or is a type mismatch.

In sum, it provides type-checking and editing assistance for users, and it determines how Customize displays the value (as a Lisp value, Boolean, etc.).

Setting a :type causes customize-variable to present an appropriate edit control for the user to customize with.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top