This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-16
Channels
- # announcements (2)
- # asami (5)
- # babashka (52)
- # beginners (42)
- # biff (3)
- # cljdoc (4)
- # cljsrn (2)
- # clojure (30)
- # clojure-austin (35)
- # clojure-dev (3)
- # clojure-france (11)
- # clojurescript (36)
- # conjure (6)
- # cursive (5)
- # fulcro (33)
- # graalvm (41)
- # lsp (54)
- # malli (1)
- # music (2)
- # off-topic (7)
- # overtone (1)
- # pedestal (5)
- # polylith (2)
- # remote-jobs (1)
- # sci (28)
- # shadow-cljs (38)
- # vim (15)
- # web-security (1)
- # xtdb (8)
After (deftype MyType ...)
, is there a difference between (MyType.)
and (->MyType)
? Why is the latter perferred?
The first is java interop calling new, the second is calling a function that calls new
The function call indirects through a var, which is more repl friendly and even allows the possibility of getting rid of the deftype, and creating your own ->MyType function that does whatever
Indirecting through the var is more repl friendly because it handles redefinition better
It's also just more portable, the former is almost "accidental", in that the deftype is implemented as a Java class. Not that it would change, but other implementations of Clojure might not honor that deftype can also then be called with (MyType.)
Where would be the canonical documentation or specification for Clojure's core java interfaces? Like, I am used to going to the java source for explanation of what something like ISeq.more
is supposed to do but there is no documentation there https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/ISeq.java#L24 I also don't see anything on it in the https://clojure.org/reference/sequences reference nor in https://clojure.github.io/clojure/clojure.core-api.html nor in https://clojure.github.io/clojure/javadoc/
for your specific question, the only place I could find that implemented more
was here https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/ASeq.java#L135-L140
and based on this implementation, my hunch was that it is used by clojure.core/rest
https://github.com/clojure/clojure/blob/clojure-1.10.1/src/clj/clojure/core.clj#L66-L73
When in doubt, look at RT.java
first. ;)
And FWIW, I'm 95% certain that there's no specification at all and 90% certain that there's no canonical documentation of all the internals. But one can occasionally find an article on such things by someone from the core team.
I just up-voted an "Ask Clojure" requesting (catch :default ...)
like in ClojureScript! https://ask.clojure.org/index.php/1953/support-try-catch-default-for-portable-catch-all?show=1953#q1953
By the way, the first-glimpse of ClojureDart also has its own way to catch "whatever".
I saw also that clojuredart uses fallback
as an extension of all types, whereas cljs uses default
Not the same semantics. satisfies?
returns false for instances supported through fallback
I did see that explicitly said. tbh it would surprise me if satisfies?
was true for protocols that extend default
but if that's the case I suppose it makes sense to differentiate
cljs.user=> (defprotocol Prot (meth [_]))
false
cljs.user=> (extend-type default Prot (meth [_] "default"))
nil
cljs.user=> (meth "a")
"default"
cljs.user=> (satisfies? Prot "a")
true