clojure

New to Clojure? Try the #beginners channel. Official docs: https://clojure.org/ Searchable message archives: https://clojurians-log.clojureverse.org/
aratare 2021-01-30T16:28:21.017500Z

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.

borkdude 2021-01-30T16:31:29.018400Z

@rextruong If you are calling a Java component from Clojure, you can just pass *out* as an argument and then .write to that

2021-02-01T16:20:32.067Z

you can use Clojure.*var*("clojure.core", "*out*"); to get the root binding of out

2021-02-01T16:21:32.067200Z

you still need to deref that though if used from java

borkdude 2021-02-01T16:33:56.067800Z

The root binding is probably not so interesting since it's just a stream to stdout

2021-02-01T16:36:56.068Z

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

borkdude 2021-02-01T16:37:40.068200Z

@noisesmith 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

2021-02-01T16:37:57.068400Z

oh - I must have misunderstood the question

2021-02-01T16:52:18.074600Z

actually, if the java code is being called from clojure, resolving *out* doesn't get the root binding, it gets the dynamic binding

2021-02-01T16:52:54.074800Z

as long as you didn't do something like call the Thread constructor and lose your binding

borkdude 2021-02-01T16:56:09.076600Z

nice

aratare 2021-01-30T16:33:27.018500Z

Unfortunately I'm not able to pass any additional info from Clojure to Java since I can't touch the method signature.

aratare 2021-01-30T16:33:56.018700Z

Though that being said I can just put it as a class property and use it instead :thinking_face:

aratare 2021-01-30T16:34:10.018900Z

Will give it a try. Thanks 🙂

aratare 2021-01-30T16:37:24.019100Z

Yep seems like a great solution. Thanks a lot 🙂

borkdude 2021-01-30T16:37:31.019300Z

:)

borkdude 2021-01-30T16:37:37.019500Z

what is a class property?

aratare 2021-01-30T16:41:07.019700Z

public class Foo {
    private int bar;
}
bar is a class property. Though my terminology may be a bit wonky.

aratare 2021-01-30T16:42:53.020100Z

Actually it's a class field.

2021-01-30T17:18:51.021200Z

Is there a way to initialize InheritableThreadLocal in all the Clojure thread pool? (Such that it will be defined for future, pmap, etc.)

borkdude 2021-01-30T17:35:54.021500Z

@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}

2021-01-30T17:38:44.022200Z

Not if the thread used was already initialized/created. In that case (.get tl) will return nil.

2021-01-30T17:39:47.022600Z

(pmap (fn [_] (.get tl)) (range 10)) can give inconsistent result.

2021-01-30T17:40:30.023200Z

Perhaps there's a way to scrap all existing threads and start fresh, once the InheritableThreadLocal is defined?

2021-01-30T17:47:30.023800Z

Why do you want that instead of, say, an atom?

2021-01-30T17:49:01.024500Z

@frozenlock ^

2021-01-30T17:52:54.026900Z

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.

alexmiller 2021-01-30T18:27:30.027700Z

You can set your own thread pool to use

alexmiller 2021-01-30T18:29:20.028700Z

And the send-off equivalent (which future uses)

alexmiller 2021-01-30T18:30:06.029700Z

I would consider these hostile in a library, but ok in an app

2021-01-30T18:33:17.030100Z

Ah, it might be what I need. Thank you!

borkdude 2021-01-30T19:51:52.031Z

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

2021-01-30T19:53:46.031200Z

Sure. It's to accumulate some debug values that can ultimately be used inside UncaughtExceptionHandler.

borkdude 2021-01-30T19:54:11.031400Z

cool

2021-01-30T19:54:30.031600Z

(context 1 .... (context 2 .... (context 3... BOOM))) <---- exception handler should be able to see the 3 contexts.

2021-01-30T19:54:39.031800Z

What was yours?

Hagenek 2021-01-30T20:02:19.033Z

Quick question for Vs code and Calva users. How do you stop evaluating when your functions enters an infinite loop?

Hagenek 2021-01-30T20:07:55.033100Z

Tried Ctrl-Alt-C Ctrl-Alt-D, but that does not seem to do the trick. (Should stop evaluations).

seancorfield 2021-01-30T20:10:09.033400Z

@georghagen FYI, there's a #calva channel where you might get answers faster for Calva-specific questions in future.

Hagenek 2021-01-30T20:10:36.033600Z

Ahh great, thanks 😃 Ill go there instead

pez 2021-01-30T20:15:14.035200Z

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.

pez 2021-01-30T20:17:15.038200Z

Ah, and that is the command you tried. Could also be that you have lots of printing in that runaway loop. It's an issue we yet have to fix. Interrupt does not stop the queued printing.

Hagenek 2021-01-30T20:18:37.038400Z

:thumbsup: At least I found why it was entering an infinite loop now

1🤘
2021-01-30T22:49:33.038900Z

Here's what I ended up, in case it could be useful https://gist.github.com/Frozenlock/e0c8b8f81838182fcf36ae14e7dfea5d

2021-01-30T22:49:42.039100Z

@borkdude

borkdude 2021-01-30T22:51:43.039500Z

thanks for sharing!