Fork me on GitHub
#clj-kondo
<
2023-08-16
>
teodorlu12:08:01

(str/starts-with? 'clojure "clojure")
;; => true
This code runs, but I get a [type-mismatch] lint message from clojure-lsp, and I’m assuming the message comes from CLJ-kondo. Is it bad to use clojure.string/start-with? with symbols without explicitly converting to strings first? If yes, why?

borkdude12:08:11

Yes, you're relying on an implementation detail. See the source:

(defn starts-with?
  "True if s starts with substr."
  {:added "1.8"}
  [^CharSequence s ^String substr]
  (.startsWith (.toString s) substr))
The argument s is a CharSequence
(instance? CharSequence 'foo)
false

👍 2
borkdude12:08:17

I guess if the type hint would have been Object then the function would still have worked, but I'm not sure if the intent is that you're throwing random objects in there. Perhaps better to ask @U064X3EF3

teodorlu12:08:27

Does that I will get better performance too with an explicit call to str, because I can avoid reflection?

teodorlu12:08:55

(Just listened to Alex’s London Clojurians talk about Clojure performance, was really interesting)

borkdude12:08:31

Well, there isn't any reflection going on, I guess .toString is just resolved to Object's toString. It's a strange edge case

👍 2
teodorlu12:08:37

I tried testing quickly, added (set! *warn-on-reflection* true) to the top of my ns, didn’t get any warnings.

teodorlu12:08:08

Thanks for the help! 🙏

Alex Miller (Clojure team)12:08:39

The intent here is that you pass it a CharSequence like a String

Alex Miller (Clojure team)12:08:14

The toString is necessary because not all CharSequences are Strings

Alex Miller (Clojure team)12:08:38

Not sure if that answers your question

👍 2
borkdude12:08:58

yep, that's how I understand it too. so throwing in a symbol isn't the intent, hence the clj-kondo warning is legit

👍 2
teodorlu13:08:18

> Not sure if that answers your question It does! 🙏