This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-01-30
Channels
- # announcements (3)
- # babashka (5)
- # beginners (68)
- # calva (34)
- # cider (6)
- # cljsrn (3)
- # clojure (48)
- # clojure-australia (2)
- # clojure-europe (1)
- # clojure-nl (4)
- # clojure-spec (28)
- # clojurescript (10)
- # conjure (20)
- # datahike (1)
- # deps-new (1)
- # depstar (27)
- # events (2)
- # fulcro (61)
- # graalvm (32)
- # membrane (18)
- # off-topic (20)
- # practicalli (10)
- # reagent (2)
- # shadow-cljs (8)
- # slack-help (20)
- # spacemacs (9)
- # sql (5)
- # vim (10)
- # xtdb (8)
Hi there. I have a setup with both Clojure and Java files where I'm using Clojure as a wrapper and Java as a lower layer to work with other things. Is there a way, from Java, to print to whatever Clojure's out
is? Thanks in advance.
@rextruong If you are calling a Java component from Clojure, you can just pass *out*
as an argument and then .write
to that
Unfortunately I'm not able to pass any additional info from Clojure to Java since I can't touch the method signature.
Though that being said I can just put it as a class property and use it instead :thinking_face:
public class Foo {
private int bar;
}
bar is a class property. Though my terminology may be a bit wonky.you can use Clojure.*var*("clojure.core", "*out*");
to get the root binding of out
you still need to deref that though if used from java
that's fair, but in order to know a specific binding, you would need to have your handle on a specific thread / context. which is implicit in clojure but it's a weird hoop to jump through if interacting with clojure via java
@U051SS2EU He is calling Java from Clojure. So you can just pass *out*
as an argument which will pass the dynamically bound value to the Java side
oh - I must have misunderstood the question
actually, if the java code is being called from clojure, resolving *out*
doesn't get the root binding, it gets the dynamic binding
as long as you didn't do something like call the Thread
constructor and lose your binding
Is there a way to initialize InheritableThreadLocal
in all the Clojure thread pool? (Such that it will be defined for future
, pmap
, etc.)
@frozenlock doesn't that already work?
user=> (def tl (proxy [java.lang.InheritableThreadLocal] [] (childValue [v] (.clone ^java.util.Map v))))
#'user/tl
user=> (.set tl (java.util.HashMap. {:a 1 :b 2}))
nil
user=> (.get tl)
{:a 1, :b 2}
user=> (.put (.get tl) :b 3)
2
user=> (.get tl)
{:a 1, :b 3}
user=> @(future (.put (.get tl) :b 4) (.get tl))
{:a 1, :b 4}
user=> (.get tl)
{:a 1, :b 3}
Not if the thread used was already initialized/created. In that case (.get tl)
will return nil.
(pmap (fn [_] (.get tl)) (range 10))
can give inconsistent result.
Perhaps there's a way to scrap all existing threads and start fresh, once the InheritableThreadLocal is defined?
Long story short: I need an inheritable dynamic value. (IE capable of transparently moving between future
, or pure java threads)
I'm pretty much there... except for existing threads in which the InheritableThreadLocal
isn't defined.
@frozenlock Can I ask you what you are using InheritableThreadLocal for? I recently discovered it and had a use case for it, but I wonder what other people are using this for.
Sure.
It's to accumulate some debug values that can ultimately be used inside UncaughtExceptionHandler
.
(context 1 .... (context 2 .... (context 3... BOOM))) <---- exception handler should be able to see the 3 contexts.
What was yours?
Here's what I ended up, in case it could be useful https://gist.github.com/Frozenlock/e0c8b8f81838182fcf36ae14e7dfea5d
You can set your own thread pool to use
http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/set-agent-send-executor!
And the send-off equivalent (which future uses)
I would consider these hostile in a library, but ok in an app
Ah, it might be what I need. Thank you!
Quick question for Vs code and Calva users. How do you stop evaluating when your functions enters an infinite loop?
Tried Ctrl-Alt-C Ctrl-Alt-D, but that does not seem to do the trick. (Should stop evaluations).
@USMLDU9BR FYI, there's a #calva channel where you might get answers faster for Calva-specific questions in future.
For anyone reading it here. There's a command in Calva for this. Not at a computer right now, but something like Interrupt running evaluations.