all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Symbol properties :prop vs 'prop
@ 2014-10-12  4:17 Paul Rankin
  0 siblings, 0 replies; 4+ messages in thread
From: Paul Rankin @ 2014-10-12  4:17 UTC (permalink / raw)
  To: help-gnu-emacs

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)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Symbol properties :prop vs 'prop
       [not found] <mailman.11026.1413087622.1147.help-gnu-emacs@gnu.org>
@ 2014-10-12  7:44 ` Pascal J. Bourguignon
  2014-10-13 10:54   ` Paul Rankin
  2014-10-14 15:33   ` sokobania.01
  0 siblings, 2 replies; 4+ messages in thread
From: Pascal J. Bourguignon @ 2014-10-12  7:44 UTC (permalink / raw)
  To: help-gnu-emacs

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Symbol properties :prop vs 'prop
  2014-10-12  7:44 ` Symbol properties :prop vs 'prop Pascal J. Bourguignon
@ 2014-10-13 10:54   ` Paul Rankin
  2014-10-14 15:33   ` sokobania.01
  1 sibling, 0 replies; 4+ messages in thread
From: Paul Rankin @ 2014-10-13 10:54 UTC (permalink / raw)
  To: help-gnu-emacs

Brilliant and informative answer, thanks Pascal :)




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Symbol properties :prop vs 'prop
  2014-10-12  7:44 ` Symbol properties :prop vs 'prop Pascal J. Bourguignon
  2014-10-13 10:54   ` Paul Rankin
@ 2014-10-14 15:33   ` sokobania.01
  1 sibling, 0 replies; 4+ messages in thread
From: sokobania.01 @ 2014-10-14 15:33 UTC (permalink / raw)
  To: help-gnu-emacs

Le dimanche 12 octobre 2014 09:44:42 UTC+2, Pascal J. Bourguignon a écrit :
> Paul Rankin 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.

You just need to be coherent of course!
ELISP> (put 'my-symbol :width 45)
45
ELISP> (get 'my-symbol :width)
45
ELISP> (get 'my-symbol 'width)
nil
ELISP> (eq 'width ':width)
nil



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-10-14 15:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.11026.1413087622.1147.help-gnu-emacs@gnu.org>
2014-10-12  7:44 ` Symbol properties :prop vs 'prop Pascal J. Bourguignon
2014-10-13 10:54   ` Paul Rankin
2014-10-14 15:33   ` sokobania.01
2014-10-12  4:17 Paul Rankin

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.