Clojure

Accept and ignore colon between key and value in map literals

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:

Description

Original title was 'treat colons as whitespace' which isn't a problem description but a (flawed) implementation approach

For JSON compatibility
known problems when no spaces - x:true and y:false

Activity

Hide
Kevin Downey added a comment -

please no

Show
Kevin Downey added a comment - please no
Hide
Tavis Rudd added a comment -

Alan Malloy raises a good point in the google group discussion (https://groups.google.com/d/msg/clojure/XvJUzaY1jec/aVpWBicwGhsJ) about accidental confusion between trailing (or floating) and leading colons:
"It isn't even as simple as "letting them
be whitespace", because presumably you want (read-string "{a: b}") to
result in (hash-map 'a 'b), but (read-string "{a :b}") to result in
(hash-map 'a :b)."

This issue could be avoided by only treating a colon as whitespace when followed by a comma. As easy cut-paste of json seems the be the key motivation here, the commas are going to be there anyway: valid {"v":, 1234} vs syntax error {a-key: should-be-a-keyword}.

Show
Tavis Rudd added a comment - Alan Malloy raises a good point in the google group discussion (https://groups.google.com/d/msg/clojure/XvJUzaY1jec/aVpWBicwGhsJ) about accidental confusion between trailing (or floating) and leading colons: "It isn't even as simple as "letting them be whitespace", because presumably you want (read-string "{a: b}") to result in (hash-map 'a 'b), but (read-string "{a :b}") to result in (hash-map 'a :b)." This issue could be avoided by only treating a colon as whitespace when followed by a comma. As easy cut-paste of json seems the be the key motivation here, the commas are going to be there anyway: valid {"v":, 1234} vs syntax error {a-key: should-be-a-keyword}.
Hide
Alex Baranosky added a comment -

This would be visually confusing imo.

Show
Alex Baranosky added a comment - This would be visually confusing imo.
Hide
Laurent Petit added a comment -

Please, oh please, no.

Show
Laurent Petit added a comment - Please, oh please, no.
Hide
Tavis Rudd added a comment -

Er, brain fart. I was typing faster than I was thinking and put the comma in the wrong place. In my head I meant the form following the colon would have to have a comma after it. Thus, {"a-json-key": 1234, ...} would be valid while {"a-json-key": was-supposed-to-be-a-keyword "another-json-key" foo} would complain about the colon being an Invalid Token. I don't see the need for it, however.

Show
Tavis Rudd added a comment - Er, brain fart. I was typing faster than I was thinking and put the comma in the wrong place. In my head I meant the form following the colon would have to have a comma after it. Thus, {"a-json-key": 1234, ...} would be valid while {"a-json-key": was-supposed-to-be-a-keyword "another-json-key" foo} would complain about the colon being an Invalid Token. I don't see the need for it, however.
Hide
Joseph Smith added a comment - - edited

Clojure already has reader syntax for a map. If we support JSON, do we also support ruby map literals? Seems like this addition would only add confusion, imo, given colons are used in keywords and keywords are frequently used in maps - e.g., when de-serializing from XML, or even JSON.

Show
Joseph Smith added a comment - - edited Clojure already has reader syntax for a map. If we support JSON, do we also support ruby map literals? Seems like this addition would only add confusion, imo, given colons are used in keywords and keywords are frequently used in maps - e.g., when de-serializing from XML, or even JSON.
Hide
David Nolen added a comment - - edited

Clojure is no longer a language hosted only on the JVM. Clojure is also hosted on the CLR, and JavaScript. In particular ClojureScript can't currently easily deal with JSON literals - an extremely common (though problematic) data format. By allowing colon whitespace in map literals - Clojure data structures can effectively become an extensible JSON superset - giving the succinctness of JSON and the expressiveness of XML.

+1 from me.

Show
David Nolen added a comment - - edited Clojure is no longer a language hosted only on the JVM. Clojure is also hosted on the CLR, and JavaScript. In particular ClojureScript can't currently easily deal with JSON literals - an extremely common (though problematic) data format. By allowing colon whitespace in map literals - Clojure data structures can effectively become an extensible JSON superset - giving the succinctness of JSON and the expressiveness of XML. +1 from me.
Christopher Redinger made changes -
Field Original Value New Value
Fix Version/s Release 1.4 [ 10040 ]
Fix Version/s Release 1.5 [ 10150 ]
Hide
Tim McCormack added a comment -

Clojure is only hosted on the JVM; ClojureScript is hosted on JS VMs. If this is useful for CLJS, it should just be a CLJS feature.

Show
Tim McCormack added a comment - Clojure is only hosted on the JVM; ClojureScript is hosted on JS VMs. If this is useful for CLJS, it should just be a CLJS feature.
Hide
Mike Anderson added a comment -

-1 for this whole idea: that way madness lies....

If we keep adding syntactical oddities like this then the language will become unmaintainably complex. It's the exact opposite of simple to have lots of special cases and ambiguities that you have to remember.

If people want to use JSON that is fine, but then the best approach use a specific JSON parser/writer, not just paste it into Clojure source and expect it to work.

Show
Mike Anderson added a comment - -1 for this whole idea: that way madness lies.... If we keep adding syntactical oddities like this then the language will become unmaintainably complex. It's the exact opposite of simple to have lots of special cases and ambiguities that you have to remember. If people want to use JSON that is fine, but then the best approach use a specific JSON parser/writer, not just paste it into Clojure source and expect it to work.
Hide
Laszlo Török added a comment -

-1 for reasons mentioned by Allan Malloy and Mike Anderson

Show
Laszlo Török added a comment - -1 for reasons mentioned by Allan Malloy and Mike Anderson
Rich Hickey made changes -
Fix Version/s Release 1.5 [ 10150 ]
Rich Hickey made changes -
Description Original title was 'treat colons as whitespace' which isn't a problem description but a (flawed) implementation approach

For JSON compatibility
known problems when no spaces - x:true and y:false
Summary Allow colon as whitespace in map literals Accept and ignore colon between key and value in map literals
Alex Miller made changes -
Labels reader

People

Vote (4)
Watch (11)

Dates

  • Created:
    Updated: