Pipes in Symbols

The pipe sign (|) is sometimes used to enclose symbols. Is there more information about when one might want or need to do this? The only example I could come up with is the empty symbol ('||) which as far as I can tell cannot be represented otherwise.

1 Like

If you have spaces in the symbol's name then it can be more convenient to use | than escaping them with \. Ditto for other characters that would normally terminate the symbol (like parentheses or semicolons).

1 Like

Yes, | is used to delimit symbols that could not otherwise be written in concrete syntax. (There are alternatives, in particular \ escapes.) Every possible string has a corresponding symbol (given string->symbol): using \ can make it much easier to write some symbols in source code.

The parsing rules for symbols are very flexible: roughly, a delimited sequence of characters parses as a symbol unless it parses as something else. That makes it a little tricky to exhaustively list cases when escapes are needed. One particularly notable case is creating symbols that would otherwise parse as numbers, such as |42| (recall that '42 instead evaluates to the number 42, so (and (symbol? '|42|) (number? '42))). Symbols containing whitespace, e.g. |Height of Giraffe|, or punctuation, e.g. |\| or |,|, can also be useful in some contexts, such as working with data or implementing domain-specific languages.

There are more details on the syntax of symbols at 1.3.2 Reading Symbols and details about | and \ specifically at 1.3.1 Delimiters and Dispatch.

1 Like

In Scribble programs I use @|a|b instead of @a b to avoid a space between a and b.

1 Like

Also in scribble: one way to document a parameter named @a is to use |@a| (this happens frequently with gui-easy programs such as https://github.com/benknoble/frosthaven-manager/blob/e8df89f71deefa0a0d4918f18c53544670b35d43/scribblings/reference.scrbl#L629, https://github.com/benknoble/frosthaven-manager/blob/e8df89f71deefa0a0d4918f18c53544670b35d43/scribblings/reference.scrbl#L856).