help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Symbol properties :prop vs 'prop


From: Pascal J. Bourguignon
Subject: Re: Symbol properties :prop vs 'prop
Date: Sun, 12 Oct 2014 09:44:42 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Paul Rankin <paul@tilk.co> writes:

> Is there any difference between naming a symbol property as :prop vs
> 'prop? i.e. what does the colon ":" mean?
>
> (put 'my-symbol :width 45)
>
> (put 'my-symbol 'width 45)

In emacs lisp, there's no difference.


In emacs lisp, symbols whose name start with a colon are named keyword,
and they are automatically set to evaluate to themselves.

   (symbol-value :hello) --> :hello
   :hello --> :hello

Notice that in emacs lisp, the colon is part of the symbol name:

   (symbol-name :hello) --> ":hello"


therefore you don't need to quote them in expressions.

The other symbols which are also interpreted as variables, often have a
value that is not themselves (notable exceptions are t and nil).
Therefore when you want to obtain the symbol in an expression, you need
to quote it:

   (defvar example 42)
   example --> 42
   'example --> example



However, in Common Lisp, there are packages and keywords are symbols
that are in the KEYWORD package, while the other symbols are in other
packages.  Notably, each library and each program use its own set of
packages, to avoid collisions and overwritting the symbols of same name
in others' packages.  CL packages are namespaces.


Now imagine that you have in the same lisp image (the same emacs
session), a library that manages vehicules (bicycles, motorcycles, cars,
trucks, etc), and let's say, a library that draws sexps with box
diagrams.

The first library wants to store the width of the vehicule in a property
named width, the other wants to store the width of the diagram
representing the sexp in a property named width.

                                                      in elisp    in CL
   (setf (getf ford :width) (meter 3.3))              collision!  collision!
   (setf (getf ford :width) (pixel 220))

                                                      in elisp    in CL
   (setf (getf ford 'vehicule:width) (meter 3.3))     no package! ok
   (setf (getf ford 'diagram:width)  (pixel 220))

                                                      in elisp    in CL
   (in-package :vehicule)                             no package! ok
   (setf (getf ford 'width) (meter 3.3))              collision!  ok
   (in-package :diagram)                              no package! ok
   (setf (getf ford 'width) (pixel 220))              collision!  ok


Therefore in elisp, you have to use prefixes all the time:

                                                      in elisp    in CL
   (setf (getf ford 'vehicule-width) (meter 3.3))     ok          ugly
   (setf (getf ford 'diagram-width)  (pixel 220))


Notice that since in elisp the colon is not a special character in
symbol names (apart from marking self-evaluating keywords), we can
actually use vehicule:width and diagram:width in elisp, using vehicule:
instead of vehicule- as prefix.

But contrarily to CL where the package qualifier is optional (a current
default package is given by the CL:*PACKAGE* variable), thes prefixes
are not optional in emacs, if you use one, you have to use them all the
time.  But this is what you hace to do in elisp, until packages are
added.


In conclusion, don't use :width or width for your symbol properties,
because other libraries use the same name for their symbol properties,
so collisions may occur.

Instead, use a prefixed symbol:    

    (setf (getf ford 'co-tilk-paul--width) 33)

-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]