Fork me on GitHub
#clojure
<
2021-09-25
>
vemv03:09:24

Can I check if a symbol resolves to a class without possibly running its class initializer?

Carlo10:09:19

library question: Is there a different idiom for this kind of code:

(let [result (computation)]
  (finalizer)
  result)
so that I can write something like
(finalizing
  (computation)
  (finalizer))

p-himik10:09:30

You can implement this on your own, with using with-open as an inspiration. Although its finalizer is hard-coded.

Carlo11:09:15

thanks p-himik, I was mostly asking if there's an idiom for this already in the standard library. If not, for my usecase I can probably just use the let, since it happens only once

p-himik11:09:55

I'm 80% sure there's nothing like that. It would require a macro, and none of the macros in clojure/core.clj do that.

p-himik11:09:08

doto would pass result to finalizer.

p-himik11:09:50

It's also more often used in the "create an object, mutate it, and return it" context, which might be confusing here.

valtteri11:09:06

True! doto indicates you’d actually do something side effecting with the results

Timur Latypoff19:09:45

May I also suggest try / finally?

💯 3
didibus23:09:05

Try/Finally is the way to go.

(try (computation)
  (finally (finalizer)))
It'll run the finally clause even if the code inside the try throws an exception, so cleanup is guaranteed to happen.

p-himik17:09:08

It would pass result to (finalizer) and replace the value of the whole form with the value returned from (finalizer).

p-himik17:09:50

Huh, Slack randomly decided to reshuffle a few characters in my message after I have already typed them. Distributed stuff is fun.

sova-soars-the-sora17:09:01

just say no to mutation 🙈

p-himik17:09:17

Sometimes it is a necessary evil, for a truly immutable program is also a truly useless one. :)

sova-soars-the-sora01:09:12

"but it's puure!!1" lol Yeah, that's true, side-effects are what we want... Mutation nohugs