Namespaced maps

Description

A common usage of namespaced keywords and symbols is in providing attribute disambiguation in map contexts:

The namespaces provide value (disambiguation) but have the downside of being repetitive and verbose.

Namespaced maps are a reader (and printer) feature to specify a namespace context for a map.

  • Namespaced maps combine a default namespace with a map and yield a map.

  • Namespaced maps are reader macros starting with #: or #::, followed by a normal map definition.

    • #:sym indicates that sym is the default namespace for the map to follow.

    • #:: indicates that the default namespace auto-resolves to the current namespace.

    • #::sym indicates that sym should be auto-resolved using the current namespace's aliases OR any fully-qualified loaded namespace.

      • These rules match the rules for auto-resolved keywords.

  • A namespaced map is read with the following differences from normal maps:

    • A keyword or symbol key without a namespace is read with the default namespace as its namespace.

    • Keys that are not symbols or keywords are not affected.

    • Keys that specify an explicit namespace are not affected EXCEPT the special namespace _, which is read with NO namespace. This allows the specification of bare keys in a namespaced map.

    • Values are not affected.

    • Nested map keys are not affected.

  • The edn reader supports #: but not #:: with the same rules as above.

  • Maps will be printed in namespaced map form only when:

    • All map keys are keywords or symbols

    • All map keys are namespaced

    • All map keys have the same namespace

Examples:

Patch: clj-1910-2.patch

Screener notes:

  • Autoresolution supports fully-qualified loaded namespaces (like auto-resolved keywords)

  • TODO: pprint support for namespaced maps

  • TODO: printer flag to suppress printing namespaced maps

Environment

None

Activity

Show:
Vitalie Spinu
August 27, 2017, 1:51 PM

Thanks for the clarification. It's NREPL issue then. I can happily set! other globals there but not this one.

import
January 20, 2018, 12:12 AM

Comment made by: idealllee

Is namespace that starts with number not allowed in namespaced maps reader macros in clojure 1.9?

user=> {:3/a 1}
#:3{:a 1}

user=> #:3{:a 1}
RuntimeException Namespaced map must specify a valid namespace: 3 clojure.lang.Util.runtimeException (Util.java:221)

I'm using clojure 1.9.0.

Although the same reader macro #:3{:a 1} is accepted in clojurescript/1.9.946

Andy Fingerhut
January 21, 2018, 10:54 AM

Clearly from your example where Clojure 1.9.0 throws an exception when attempting to read the input #:3{:a 1}, it does not accept a namespace that starts with a number.

Perhaps your question is: "Is this a bug in Clojure 1.9.0, or intended behavior?"

Andy Fingerhut
January 21, 2018, 10:56 AM

Chenyu Li, you may want to consider creating a new JIRA issue with that question, since this ticket is already closed as resolved, and such a question in a comment here is more likely to go unnoticed.

import
January 22, 2018, 9:25 PM

Comment made by: idealllee

Thanks Andy. Created an issue here per your suggestion: https://dev.clojure.org/jira/browse/CLJ-2315

Assignee

Alex Miller

Reporter

Alex Miller

Labels

Approval

Ok

Patch

Code and Test

Fix versions

Priority

Critical
Configure