5

I am reading the cl-fad/load.lisp code tonight, and I found there are symbols #+: and #-: in the front of expression or string.

What's these symbols meaning?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
ccQpein
  • 705
  • 7
  • 20

2 Answers2

7

These are a read-time conditionalization facility: #+ and #- let you decide what expression to read based on Feature Expressions.

E.g.,

#+:allegro (require :osi)
#+:sbcl (require :sb-executable)

means that when running under allegro, the module :osi will be loaded, but when running under sbcl, the module :sb-executable will be loaded by require.

Under all other implementations require will not be called at all because read will skip over the forms.

You can check not just the implementation name, but a specific feature, e.g.,

#+(<= (integer-length most-positive-fixnum) 32)
code for a 32-bit lisp
#+(> (integer-length most-positive-fixnum) 32)
code for a 64-bit lisp

In addition to selecting code based on implementation, this allows one to easily "comment out" a section of your code (i.e., the next sexp):

#+(or) (this code will be skipped over by any lisp reader
        because (or) returns nil)
sds
  • 58,617
  • 29
  • 161
  • 278
  • 1
    For the "commenting out" I've also found `#+nil` to work. –  Oct 26 '16 at 11:59
  • @rogual: it only works because you are not using the [NIL](https://en.wikipedia.org/wiki/NIL_(programming_language)) – sds Oct 26 '16 at 12:56
2

This are reader macros based on the list features, this macros indicates to execute a form or not if the symbol is present on the features list

Showing the features list:

CL-USER> *features*
(:SWANK :QUICKLISP :QUICKLISP-SUPPORT-HTTPS :ROS.INIT :ASDF-PACKAGE-SYSTEM :ASDF3.1 :ASDF3 :ASDF2 :ASDF :OS-MACOSX :OS-UNIX :ASDF-UNICODE :PRIMARY-CLASSES :COMMON-LISP :OPENMCL :CCL :CCL-1.2 :CCL-1.3 :CCL-1.4 :CCL-1.5 :CCL-1.6 :CCL-1.7 :CCL-1.8 :CCL-1.9 :CCL-1.10 :CCL-1.11 :CLOZURE :CLOZURE-COMMON-LISP :ANSI-CL :UNIX :OPENMCL-UNICODE-STRINGS :IPV6 :OPENMCL-NATIVE-THREADS :OPENMCL-PARTIAL-MOP :MCL-COMMON-MOP-SUBSET :OPENMCL-MOP-2 :OPENMCL-PRIVATE-HASH-TABLES :STATIC-CONSES-SHOULD-WORK-WITH-EGC-IN-CCL :X86-64 :X86_64 :X86-TARGET :X86-HOST :X8664-TARGET :X8664-HOST :DARWIN-HOST :DARWIN-TARGET :DARWINX86-TARGET :DARWINX8664-TARGET :DARWINX8664-HOST :64-BIT-TARGET :64-BIT-HOST :DARWIN :LITTLE-ENDIAN-TARGET :LITTLE-ENDIAN-HOST)

In my case I 'm running:

CL-USER> (lisp-implementation-type)
"Clozure Common Lisp"

CL-USER> (lisp-implementation-version)
"Version 1.11-r16635  (DarwinX8664)"

Let's execute the form if I'm using CCL

CL-USER> #+CCL (1+ 1)
2

It works, because I have CCL on the features list

CL-USER> #-CCL (1+ 1)
; No value

It doest work because I have CCL in the features list

Or you can think the oppsite,only execute if I dont' have in teh features list

CL-USER> #-calimero (1+ 1)
2

You can ad any symbol :word to the features list and you can also add logic

let's execute if I'm on the CCL and using a darwin host (i.e MAC OS X)

CL-USER> #+(and ccl darwin-host) (1+ 1)
anquegi
  • 11,125
  • 4
  • 51
  • 67