This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-20
Channels
- # announcements (33)
- # aws (1)
- # babashka (8)
- # beginners (100)
- # calva (59)
- # clara (4)
- # clj-kondo (33)
- # cljdoc (9)
- # cljs-dev (30)
- # cljsrn (1)
- # clojure (28)
- # clojure-australia (1)
- # clojure-boston (1)
- # clojure-dev (4)
- # clojure-europe (14)
- # clojure-france (5)
- # clojure-italy (7)
- # clojure-nl (1)
- # clojure-uk (36)
- # clojurescript (13)
- # clojureverse-ops (6)
- # conjure (2)
- # cursive (2)
- # datahike (11)
- # datalevin (1)
- # datomic (106)
- # graphql (3)
- # helix (10)
- # holy-lambda (24)
- # kaocha (2)
- # lambdaisland (3)
- # lsp (199)
- # malli (35)
- # off-topic (16)
- # pathom (7)
- # polylith (38)
- # portal (16)
- # quil (2)
- # re-frame (18)
- # reagent (57)
- # shadow-cljs (11)
- # testing (3)
- # xtdb (9)
reify might have the worst doc-string of them all: > reify is a macro with the following structure: > (reify options* specs*)
Huh?
Clojure 1.10.3
user=> (doc reify)
-------------------------
clojure.core/reify
([& opts+specs])
Macro
reify is a macro with the following structure:
(reify options* specs*)
Currently there are no options.
Each spec consists of the protocol or interface name followed by zero
or more method bodies:
protocol-or-interface-or-Object
(methodName [args+] body)*
Methods should be supplied for all methods of the desired
protocol(s) and interface(s). You can also define overrides for
methods of Object. Note that the first parameter must be supplied to
correspond to the target object ('this' in Java parlance). Thus
methods for interfaces will take one more argument than do the
interface declarations. Note also that recur calls to the method
head should *not* pass the target object, it will be supplied
automatically and can not be substituted.
The return type can be indicated by a type hint on the method name,
and arg types can be indicated by a type hint on arg names. If you
leave out all hints, reify will try to match on same name/arity
method in the protocol(s)/interface(s) - this is preferred. If you
supply any hints at all, no inference is done, so all hints (or
default of Object) must be correct, for both arguments and return
type. If a method is overloaded in a protocol/interface, multiple
independent method definitions must be supplied. If overloaded with
same arity in an interface you must specify complete hints to
disambiguate - a missing hint implies Object.
recur works to method heads The method bodies of reify are lexical
closures, and can refer to the surrounding local scope:
(str (let [f "foo"]
(reify Object
(toString [this] f))))
== "foo"
(seq (let [f "foo"]
(reify clojure.lang.Seqable
(seq [this] (seq f)))))
== (\f \o \o))
reify always implements clojure.lang.IObj and transfers meta
data of the form to the created object.
(meta ^{:k :v} (reify Object (toString [this] "foo")))
== {:k :v}
nil
user=>
Ya, it goes into some details, but the summary is: > reify is a macro with the following structure:
Right. Time to create an http://ask.clojure.org question with a suggested edit? :)
There is an active ticket for this already
I do hope the docs get improved for this one. I can't figure out how to implement reify for multiple interfaces
I'm not a junior dev. I have been using clojure on and off for some almost two years, but I can't figure out by reading documentation - how to use reify to implement multiple interfaces. IMO the docs can be improved.
FWIW I think the docs are rather clear. The only thing I'd add is, perhaps, an example of the above.
Although Clojure code itself has such examples - future-call
, promise
, iteration
.
hi, there was an issue - I tried to add docstring and it does not support it. Once I removed method docstring it worked. I added a note to https://clojuredocs.org/clojure.core/reify about this . hopefully people won't fell for the same trap 🙂
I am debugging something. I have a TransientHashSet I must inspect. I cannot call persistent!
because the library exposing the TransientHashSet expects to continue modifying it. What can I do?
If It were a TransientVector I could call nth
on it but since it is a set I cannot
Do exposed Java methods work for inspection?
https://github.com/clojure/clojure/blob/b8132f92f3c3862aa6cdd8a72e4e74802a63f673/src/jvm/clojure/lang/PersistentHashSet.java#L120 It looks like it extends ATransientSet
, so you can call things like .count
and .contains
...
Hacky solution
(into []
(keep-indexed (fn [idx v]
(when (even? idx)
v)))
(.-arr ^TransientArrayMap (.-transient_map ^TransientHashSet s)))
Hi! Is it intended that sorted-map-by
freaks out if the comparator is weird? I'd expect the output to be unstable, but not for it to be downright missing data from the input.
user=> (defn kinda> [a b] (> (max a 2) (max b 2)))
#'user/kinda>
user=> (sorted-map-by kinda> 1 "a", 2 "b", 3 "c")
{3 "c", 1 "b"}
It is documented: https://clojure.org/guides/comparators
it is a comparator issue, I just get confused about sorted-map-by, because the -by means something different from the by in sort-by
what is the complexity of count
on a vector? O(n)? Or is there something like the length of the vector stored somewhere that allows O(1) ?
you can use counted?
to determine if coll
implements count
in constant time
> (counted? [1 2 3])
true