Fork me on GitHub
#clojure
<
2022-04-16
>
zhuxun200:04:39

After (deftype MyType ...), is there a difference between (MyType.) and (->MyType)? Why is the latter perferred?

hiredman00:04:03

The first is java interop calling new, the second is calling a function that calls new

zhuxun200:04:15

Sounds like the second is worse?

hiredman00:04:16

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

hiredman00:04:26

No, the second is better

hiredman00:04:10

Indirecting through the var is more repl friendly because it handles redefinition better

👍 2
zhuxun200:04:23

Got it. Makes sense.

didibus05:04:49

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.)

☝️ 1
emccue16:04:10

TIL you can serialize and deserialize Vars

JoshLemer16:04:41

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/

lilactown18:04:24

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

lilactown18:04:58

and based on this implementation, my hunch was that it is used by clojure.core/rest

p-himik18:04:07

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.

❤️ 1
phill18:04:52

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".

👍 2
ghadi01:04:08

why isn't catch Throwable sufficient?

phill09:04:02

Does it work in ClojureScript?

lilactown19:04:56

I saw also that clojuredart uses fallback as an extension of all types, whereas cljs uses default

cgrand19:04:21

Not the same semantics. satisfies? returns false for instances supported through fallback

lilactown19:04:15

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

cgrand19:04:52

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

1
lilactown19:04:26

do you think you'll implement default as well?

cgrand20:04:16

well Cljs had to implement default because there’s no “root type” in Js. In Cljd you can just extdend to Object and null like in Clojure

_II_19:04:07

Does anyone uses graalvm to make native image ?

littleli21:04:09

we have a channel here #graalvm

_II_19:04:32

it's fine until I try to use any db driver

_II_19:04:42

mysql, mongo, mariadb ...