7.7
quote - bad
source code: https://github.com/AlexKnauth/quote-bad
2 Changing the printing style to avoid printing bad uses of quote |
1 Replacing bad uses of quote in programs
(require quote-bad/quote-bad) | package: quote-bad |
The quote form can seem convenient as a shorthand, but it can
lead to many common mistakes. Most of these mistakes come from people
not understanding that a quoted form also quotes its sub-expressions.
In a file that requires this library, using quote for
compound data will cause an error with a message telling you how to
write it without quote, and "self-quoting" compound data
literals such as vector literals will display a similar error
explaining how to write the same thing without them.
Examples:
> (require quote-bad/quote-bad) > 'hello 'hello
> '"this is fine" "this is fine"
> '#:of-course '#:of-course
> '(1 2 (+ 1 2)) eval:7:0: quote: Don't use quote for this. Instead you can
use
(list 1 2 (list '+ 1 2))
in: (quote (1 2 (+ 1 2)))
> (list 1 2 (list '+ 1 2)) (list 1 2 (list '+ 1 2))
> (list 1 2 (+ 1 2)) (list 1 2 3)
> #(1 2 (+ 1 2)) eval:10:0: #%datum: Don't use self-quoting compound literals
that implicitly quote sub-expressions.
Instead you can use
(vector-immutable 1 2 (list '+ 1 2))
in: (#%datum . #(1 2 (+ 1 2)))
> (vector-immutable 1 2 (list '+ 1 2)) (vector-immutable 1 2 (list '+ 1 2))
> (vector-immutable 1 2 (+ 1 2)) (vector-immutable 1 2 3)
Unfortunately, this doesn’t do anything about printing (yet), so
depending on your settings, values like lists, vectors, etc. could
still print with quote.
Note that this does not do anything to quasiquote, so if
there any expressions that really do make sense as quoted, you can
quasiquote them instead.
syntax
(quote datum)
Like racket’s quote form, except that for compound literal data it
displays an error message explaining how to write the expression
without quote.
syntax
(#%datum . datum)
A form implicitly inserted for literals. This version expands to
quote for simple atomic data such as numbers, strings, and
booleans, but for self-quoting compound data such as vector literals,
it displays an error message explaining how to write the expression
without using self-quoting literals.
2 Changing the printing style to avoid printing bad uses of quote
#lang constructor-style-print | package: quote-bad |
A lang-extension like at-exp that changes the printing style
for programs that use it in the main file. The lang line #lang
constructor-style-print racket
declares a language like racket, except that values
are printed using constructor-style printing instead of with
quote.
Instead of '(1 2 3), a list would print as
(list 1 2 3). Instead of '#(1 2 3), a mutable vector
would print as (vector 1 2 3), and an immutable vector would
print as (vector-immutable 1 2 3).
But most importantly, a nested structure like '(1 2 (+ 1 2))
would print as (list 1 2 (list '+ 1 2)), showing the nested
uses of the constructors.
Examples:
> (require racket/vector) > 'hello 'hello
> (list 1 2 (+ 1 2)) (list 1 2 3)
> '(1 2 (+ 1 2)) (list 1 2 (list '+ 1 2))
> (vector-immutable 1 2 (+ 1 2)) (vector-immutable 1 2 3)
> #(1 2 (+ 1 2)) (vector-immutable 1 2 (list '+ 1 2))
> (vector 1 2 (+ 1 2)) (vector 1 2 3)
> (vector-copy #(1 2 (+ 1 2))) (vector 1 2 (list '+ 1 2))