-
Notifications
You must be signed in to change notification settings - Fork 161
Reader extension: |‐quoting
ClojureCLR extends the reader syntax for symbols by the mechanism of vertical bar quoting.
Vertical bars are used in pairs to surround the name (or part of the name) of a symbol that has any special characters (from the standard Clojure symbol viewpoint) in it. For example, |A(B)|
, A|(|B|)|
, and A|(B)|
all mean the symbol whose name consists of the four characters A, (, B, and ).
|anything except a vertical bar...<>@#$@#$#$|
To include a vertical bar in a symbol name that is |-quoted, use a doubled vertical bar.
|This has a vertical bar in the name ... || ...<>@#$@#$#$|
There is a special interaction of |-quoting with / used to separate namespace from name. Any / appearing |-quoted does not count as a namespace/name separator. Thus,
(namespace 'ab|cd/ef|gh) ;=> nil
(name 'ab|cd/ef|gh) ;=> "abcd/efgh"
(namespace 'ab/cd|ef/gh|ij) ;=> "ab"
(name 'ab/cd|ef/gh|ij) ;=> "cdef/ghij"
Though the vertical bars need only surround troublesome characters, it may be more readable to quote the entire namespace name and or symbol name. Thus, for the two examples immediately above, you might prefer
|abcd/efgh|
instead ofab|cd/ef|gh
ab/|cdef/ghij|
instead ofab/cd|ef/gh|ij
With this mechanism we can make a symbol referring to a type such as: (|com.myco.mytype+nested, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b14a123334343434|/DoSomething x y)
or
(reify
|AnInterface`2[System.Int32,System.String]|
(m1 [x] ...)
I2
(m2 [x] ...))
Note: |-quoting defined in ClojureCLR differs from the similar mechanism in CommonLisp in one significant way:
- CommonLisp allows a literal vertical bar in a symbol name with backslash-escaping:
abc\|123
has name “abc|123”. ClojureCLR uses a doubled vertical bar, and only within an |-quoted symbol name. We would write|abc||123|
for the symbol with name “abc|123”.
Note: |-quoting prevents characters so quoted from stopping the scan of a token. However, post-scanning steps for violations such as starting with a digit or containing a non-initial colon are still performed.
Note: If printing a symbol with *print-dup*
true, a symbol with namespace name or symbol name that contain 'bad' characters will be |-quoted.
Note: To turn off |-quoting just bind the dynamic Var *allow-symbol-escape*
to false and invoke the reader in that context.