This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-05-16
Channels
- # announcements (1)
- # aws (1)
- # babashka (14)
- # beginners (25)
- # biff (5)
- # calva (6)
- # clj-kondo (3)
- # cljsrn (7)
- # clojars (7)
- # clojure (26)
- # clojure-europe (13)
- # clojurescript (10)
- # code-reviews (1)
- # cursive (9)
- # datahike (3)
- # datomic (7)
- # depstar (5)
- # emacs (9)
- # garden (2)
- # graalvm (1)
- # helix (3)
- # jobs (1)
- # leiningen (2)
- # off-topic (1)
- # pathom (3)
- # re-frame (16)
- # reitit (3)
- # releases (1)
- # shadow-cljs (10)
- # spacemacs (6)
- # tools-deps (16)
I noticed that Clojure has a BIG incentive to write small functions - you cannot write long imperative walls of text here because every new binding (`let`, loop
, etc.) increases nesting level
Some people get around that by writing all of their imperative code inside the let binding vector 😉
Here's one: https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L225-L261
i've tried. with-meta won't tag array types for me...
(defmacro ->typed-array
[klass things]
`(with-meta
(into-array ~klass ~things)
{:tag (str "[L" ~(.getName klass) ";")}))
(->typed-array Map []
ClassCastException class [Ljava.util.Map; cannot be cast to class clojure.lang.IObj ([Ljava.util.Map; is in module java.base of loader 'bootstrap'; clojure.lang.IObj is in unnamed module of loader 'app')
i've tried. with-meta won't tag array types for me...
(defmacro ->typed-array
[klass things]
`(with-meta
(into-array ~klass ~things)
{:tag (str "[L" ~(.getName klass) ";")}))
(->typed-array Map []
ClassCastException class [Ljava.util.Map; cannot be cast to class clojure.lang.IObj ([Ljava.util.Map; is in module java.base of loader 'bootstrap'; clojure.lang.IObj is in unnamed module of loader 'app')
arrays, as Java objects, have no place to put meta
it's weird that the compiler knows it's a java object that can't take meta but still demands type hints for it
you're confusing what's known at compile-time and run-time
what you're really looking for is something that sets the return type on the into-array expression (the code, which is a list) such that the compiler can resolve the surrounding Java interop call at compile time into a non-reflective call
(defmacro ->typed-array
[klass things]
(let [^Class resolved (resolve klass)]
(with-meta
(list 'into-array resolved things)
{:tag (str "[L" (.getName resolved) ";")})))
was just going to post that :)
well it wouldn't be a function then
so that's not a thing we could change
but could be a new wrapper macro
@vale a little snippet how to get array classes programmatically
(let [classes (apply list java.lang.String java.lang.Object (map (comp eval symbol second) primitives-classnames))]
(map #(.. (do %) (arrayType) (getName)) classes))
=> ("[Ljava.lang.String;" "[Ljava.lang.Object;" "[F" "[I" "[J" "[Z" "[C" "[D" "[B" "[S")