inconsistency in (keyword…) constructor

70 views
Skip to first unread message

Frank Siebenlist

unread,
Mar 2, 2012, 1:42:01 AM3/2/12
to Clojure, Frank Siebenlist
The keyword constructor has two signatures: [name] and [ns name]

The keyword doc reads: "Returns a Keyword with the given namespace and name. Do not use :
in the keyword strings, it will be added automatically."

Looking at the implementation and testing in the repl, for [name] the following works:

user=> (keyword "abc")
:abc
user=> (keyword 'abc)
:abc
user=> (keyword :abc)
:abc
user=>

However, for the second signature [ns name], only the arguments as string work:

user=> (keyword "my-ns" "abc")
:my-ns/abc
user=> (keyword "my-ns" 'abc)
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String clojure.core/keyword (core.clj:558)
user=> (keyword "my-ns" :abc)
ClassCastException clojure.lang.Keyword cannot be cast to java.lang.String clojure.core/keyword (core.clj:558)
user=> (keyword 'my-ns "abc")
ClassCastException clojure.lang.Symbol cannot be cast to java.lang.String clojure.core/keyword (core.clj:558)
user=> (keyword *ns* "abc")
ClassCastException clojure.lang.Namespace cannot be cast to java.lang.String clojure.core/keyword (core.clj:558)
user=>

The java implementation of clojure.lang.Keyword/intern shows that only a function signature of (String, String) is supported for the call made in [ns name].

The fairly small patch at: "https://gist.github.com/1956218" for clojure.core/keyword would add some conversions-to-string in the [ns name] code to allow for different data types on par with [name], and would also allow you to specify the ns as string/symbol/namespace.

In Assembla I couldn't find any bug report related to this (?).

Would this be considered a (usability) bug or just too much perfectionism?

Regards, Frank.

PS. same issue with (symbol name) and (symbol ns name)…

joegallo

unread,
Mar 2, 2012, 12:32:22 PM3/2/12
to clo...@googlegroups.com, Frank Siebenlist

PS. same issue with (symbol name) and (symbol ns name)…


Huh, somehow I noticed this issue with symbol, but didn't see it with keyword.  I guess I've need the two arg version of symbol, but never needed the two arg version of keyword.

See some related discussion here: https://groups.google.com/d/topic/clojure/n25aZ5HA7hc/discussion

Frank Siebenlist

unread,
Mar 2, 2012, 5:59:28 PM3/2/12
to joegallo, Frank Siebenlist, clo...@googlegroups.com
Just found your CLJ-891 and added a comment there that keyword has similar issues with a link to this thread.

See that you tried to solve it in java… while I tried to stay up in clj … but any protocol-based solution seems to be up in the air still (?).

Hopefully both interfaces can be enhanced in silmilar ways.

-FrankS.

Reply all
Reply to author
Forward
0 new messages