This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-08-16
Channels
- # announcements (25)
- # babashka (15)
- # beginners (14)
- # calva (60)
- # circleci (1)
- # clerk (1)
- # clj-kondo (13)
- # cljdoc (7)
- # clojure (45)
- # clojure-austin (1)
- # clojure-bay-area (1)
- # clojure-brasil (4)
- # clojure-dev (9)
- # clojure-europe (24)
- # clojure-norway (105)
- # clojure-uk (2)
- # clojurescript (6)
- # conjure (1)
- # core-typed (4)
- # cursive (4)
- # datalevin (1)
- # datomic (25)
- # emacs (31)
- # fulcro (3)
- # humbleui (10)
- # hyperfiddle (19)
- # jobs (2)
- # luminus (3)
- # malli (13)
- # nbb (5)
- # off-topic (16)
- # polylith (2)
- # portal (7)
- # releases (2)
- # shadow-cljs (5)
- # sql (8)
(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?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
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
Does that I will get better performance too with an explicit call to str
, because I can avoid reflection?
(Just listened to Alex’s London Clojurians talk about Clojure performance, was really interesting)
Well, there isn't any reflection going on, I guess .toString
is just resolved to Object's toString
. It's a strange edge case
I tried testing quickly, added (set! *warn-on-reflection* true)
to the top of my ns, didn’t get any warnings.
The intent here is that you pass it a CharSequence like a String
The toString is necessary because not all CharSequences are Strings