unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Symbol properties :prop vs 'prop
Date: Sun, 12 Oct 2014 09:44:42 +0200	[thread overview]
Message-ID: <87k3454tqt.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.11026.1413087622.1147.help-gnu-emacs@gnu.org

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	other threads:[~2014-10-12  7:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.11026.1413087622.1147.help-gnu-emacs@gnu.org>
2014-10-12  7:44 ` Pascal J. Bourguignon [this message]
2014-10-13 10:54   ` Symbol properties :prop vs 'prop Paul Rankin
2014-10-14 15:33   ` sokobania.01
2014-10-12  4:17 Paul Rankin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87k3454tqt.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).