clojure-dev

souenzzo 2023-06-07T10:09:01.099669Z

keyword/symbol constructor behavior with nil is pretty inconsistent:

(keyword nil)
=> nil
(symbol nil)
Execution error (IllegalArgumentException) at user/eval7552 user.clj:1).
no conversion to symbol
(keyword nil nil)
Execution error (NullPointerException) at java.util.concurrent.ConcurrentHashMap/get (ConcurrentHashMap.java:936).
Cannot invoke "String.hashCode()" because "this.name" is null
(symbol nil nil)
=> null
1. should (keyword nil nil) have a better error message? The current one is pretty internal. 2. (symbol nil nil) generates null?! It should behave like keyword and throw. 3. (symbol nil) should return nil, as (keyword nil) ?! In case of "please open an ask question", should it be 1 single question? or split in 3?

Alex Miller (Clojure team) 2023-06-07T13:03:14.035179Z

I think all of this falls into the zone of “don’t do that, it is undefined”

🆗 1
Alex Miller (Clojure team) 2023-06-07T13:04:13.998819Z

Adding guards here affects construction of every keyword and symbol so these are not free

Alex Miller (Clojure team) 2023-06-07T13:06:10.829849Z

I guess I am wondering if there is something that happened where you encountered this, then I’m more interested in what that was

souenzzo 2023-06-07T13:08:13.334759Z

all this started with an empty stacktrace. then I added -XX:-OmitStackTraceInFastThrow, got this NullPointerException, after look the stacktrace, i found the root cause (keyword "a" nil)

Alex Miller (Clojure team) 2023-06-07T13:11:24.125839Z

And how you ended up with that is the more interesting question

souenzzo 2023-06-07T13:17:07.467639Z

Inside a function defn add-kw-suffix [kw suffix]

(if (qualified-ident? k)
  (keyword (namespace k)
    (str (name k)
      (when suffix
        (str "-" suffix))))
  (keyword
    (str (name k)
      (when suffix
        (str "-" suffix)))))
But I wrote it wrong:
(keyword (namespace k)
  (when suffix
    (str "-" suffix)))

Alex Miller (Clojure team) 2023-06-07T13:36:50.465699Z

and then you got an NPE I assume

👍 1
Alex Miller (Clojure team) 2023-06-07T13:38:13.933979Z

seems like the desired behavior to me

2023-06-07T17:20:28.328429Z

The symbol case is super funky here though, because

user=> (symbol "hello" nil)
hello/null

2023-06-07T17:20:52.371589Z

Obviously this is a case of Should Not Happen™, but definitely an edge case to be aware of.