Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by

In Clojure 1.10.1 using numbers in qualified keywords results in a Syntax error

user> :ns/1
Syntax error reading source at (REPL:136:1).
Invalid token: :ns/1

I can however construct it using keyword

users> (keyword "ns" "1")

My problem is that the second form is not a legal test expression to use in a case statement.

1 Answer

+2 votes
selected by
Best answer

Note: I am not a person who is authorized to make decisions on what the Clojure language is, or is not, but a close observer for multiple years hoping to provide some background information.

This is historically a bleeding edge case in Clojure, asked about many times, but it appears maybe not on ask.clojure.org before.

Interpretations have differed on the precise meaning of the official docs here https://clojure.org/reference/reader but it does say there that "Symbols begin with a non-numeric character" and "Keywords are like symbols, except ... They can and must begin with a colon".

The function keyword allows one to construct values of type keyword that the Clojure reader cannot read. If you want a variant of keyword that restricts you to only constructing keyword values that the Clojure reader can read, probably the best way is to write a function that (a) constructs the keyword using keyword, and (b) converts that to a string, and (c) attempts to read it with the Clojure reader, returning some kind of error status, throwing an exception, or whatever you want if the resulting keyword is unreadable.

There was a ticket CLJ-1252 https://clojure.atlassian.net/browse/CLJ-1252 where they temporarily changed the Clojure reader to disallow reading of some keywords that had leading digits, but rolled that back because some libraries out in the wild were already relying upon the behavior, and they did not want to break backwards compatibility for this issue.

There is a faq entry about this at https://clojure.org/guides/faq#keyword_number