What are the allowed characters in a Clojure keyword?

后端 未结 4 2128
挽巷
挽巷 2021-02-19 01:27

I am looking for a list of the allowed characters in a clojure keyword. Specifically I am interested to know if any of the following characters are allowed: -

4条回答
  •  感情败类
    2021-02-19 02:06

    Edit:

    When I initially composed this answer, I was probably a little too heavily invested in the question of "what can you get away with?" In fairness to myself though, the keyword admissibility issue appears to be unsettled still. So:

    First, a little about keywords, for new readers:

    • Keywords come in two flavours, qualified and unqualified. Unqualified keywords, like :foo, have no namespace component. Qualified keywords look like :foo/bar where the part prior to the slash is the namespace, ostensibly. Keywords can't be referred, and can be given a non-existent namespace, so their namespace behaviour is different from other Clojure objects.
    • Keywords can be created either by literals to the reader, like :foo, or by the keyword function, which is (keyword name-str) or (keyword ns name).
    • Keywords evaluate to themselves only, unlike symbols which point to vars. Note that keywords are not symbols.

    What is officially permitted?

    According to the reader documentation a single slash is permitted, a no periods in the name, and all rules to do with symbols.

    What is actually permitted?

    More or less anything but spaces seem to be permitted in the reader. For instance,

    user> :-_./asdfgse/aser/se
    :-_./asdfgse/aser/se
    

    Appears to be legal. The namespace for the above keyword is:

    user> (namespace :-_./asdfgse/aser/se)
    "-_./asdfgse/aser"
    

    So the namespace appears to consist of everything prior to the last forward slash.

    The keyword function is even more permissive:

    user> (keyword "////+" "/////")
    :////+//////
    user> (namespace (keyword "////+" "/////"))
    "////+"
    

    And similarly, spaces are fine too if you use the keyword function. I'm not sure exactly what limitations are placed on Unicode characters, but the REPL doesn't appear to complain when I put in arbitrary characters.

    What's likely to happen in the future:

    There have been some rumblings about validating keywords as they are interned. Supposedly one of the longest open clojure tickets is concerned with validation of keywords. So the keyword function may cease to be so permissive in the future, though that seems to be up in the air. See the assembla ticket and google group discussion.

提交回复
热议问题