Fork me on GitHub
#clojure
<
2020-08-02
>
fricze07:08:49

Hi, good day everyone! I’m trying to get my feet wet with Clojure internals. Currently I’m reading iterate implementation in src/jvm/clojure/lang/Iterate.java and it caught my attention that _seed and _next are volatile variables. I’ve also seen that in first implementation seed was just final and got changed to volatile. Could someone explain a bit, or show me a path to understand why is that? Why it’s important for _seed and _next to have always the same value visible in all threads? What would happen if that wasn’t the case? Is that anyhow connected with laziness?

vemv07:08:58

👋 ! Did you check https://github.com/clojure/clojure/commits/master/src/jvm/clojure/lang/Iterate.java ? That should answer the first question (specifically, see how commits are linked to JIRA tickets)

vemv07:08:14

> Why it’s important for seed and next to have always the same value visible in all threads? I think this question answers itself... :) imagine if this wasn't the case and that (take 10 an-iterate-object) returned different results depending on the thread you're on. That would seem very broken

fricze07:08:41

hi! thanks for response 🙂 I checked commits, yes 🙂 I’m just starting to understand multithreading and I lack proper mental models, so it’s hard for me to imagine particular situation and particular error that could happen because those variables were not volatile

vemv07:08:34

Sure :) rich recommends the JCIP book, I can second that; with it the 'why' for volatile etc will come more naturally

👍 3
leonoel11:08:49

that's a good question

Alex Miller (Clojure team)12:08:54

Writes to non volatile fields are not guaranteed to be seen by other threads

👍 6
Nathan K08:08:44

any vscode / calva users? How do I stop a command in the repl? lol.. it's just looping

practicalli-johnny09:08:35

There is a #calva channel if you don’t get an answer here

zebu10:08:11

next.jdbc question - where can i specify the builder-fn in here? Want to get lowercase qualified keys

(with-open [conn (jdbc/get-connection config/h2-spec)]
    (sql/query conn ["select * from connections"]))

zebu10:08:21

Got it myself

(with-open [conn (jdbc/get-connection config/h2-spec)]
  (sql/query conn ["select * from connections"] {:builder-fn rs/as-lower-maps}))

👍 3
Kevin16:08:23

Hi, is there some kind of "in memory" file / buffer that can act as a file? Basically want to test reading files, but I don't want to actually create any on the filesystem.

noisesmith17:08:33

is using files part of the end user contract?

noisesmith17:08:58

if not, I strongly disrecommend using files explicitly, and instead build things to use inpustream / outputstream

noisesmith17:08:07

Even if files are part of the end user experience, it's much easier to use i/o streams internally, and make the fact that it's a file a runtime implementation detail. The JVM already guarantees you can do the same things with file streams you can do with other streams, that doesn't need special test coverage.

Kevin17:08:08

it is for the enduser. And I've split my code in two parts. One that reads the file, the other that does the processing. And I can do most of my testing on the second part, but I still want to write a few tests for the first

Kevin16:08:49

Oh looks like this works (let [a (.ByteArrayOutputStream.)] (spit a "foo") (slurp (.toByteArray a)))

Kevin16:08:00

If there's a cleaner way then I'm all open for suggestions

sveri18:08:45

Hi, I have a set of tests (using clojure.test), all in one namespace that have an each fixture declared. Now I want to run the same set of tests unchanged (same input / output and assertions) twice, where the only difference is that in the each function, a different file is loaded for the second run. I guess there is a way to do that, but cannot come up with one right now that is simple, so, I'd be interested in ideas in how to solve that. It would also be great to see, if a test fails, with which precondition it failed.

borkdude18:08:59

@sveri About the only place where I use dynamic vars is with clojure.test-related code. Might also work in this case.

sveri18:08:21

Would it bea idiomatic to run the test function twice? Something like:

(defn database-setup [f]
  (create-test-specs)
  (f)
  (create-test-specs-without-paths)
  (f))

sveri18:08:45

where database-setup is the each fixture.

jsn18:08:31

@sveri are you aware of test-ns-hook ?

sveri18:08:35

@jason358 No, thank you, I will have a look

sveri19:08:59

-.- I am using kaocha, which has an open issue for support of test-ns-hook https://github.com/lambdaisland/kaocha/issues/29 So unless I change my test runner I will have to look for a different solution.

λraulain20:08:14

Hi everyone. As an experienced Clojurist, how often do you use https://twitter.com/hashtag/Clojure?src=hashtag_click's (comment ) form in your day to day development? Do you have any other use case for it other than trying out something during development? Do you later delete it or do you push it along to production?

Alex Miller (Clojure team)20:08:49

I use it every day as a place to evaluate code into my repl. I often leave the blocks in committed code for the next time I'm working there

thanks3 3
💯 6
hadils21:08:50

Anyone with experience with depstar?

seancorfield21:08:45

@hadilsabbagh18 We use it at work to build all our deployment artifacts. And I maintain it.

hadils21:08:13

Hi @seancorfield! I figured out the problem I was having. Sorry to bother you...

3
seancorfield21:08:03

@kraulain I use (comment ..) every day. I leave a lot of the comment blocks in the code when it is committed. I use it both to explore the problem space and the solution space, as well as to provide "set up" code for use with the REPL. You'll see that sort of thing in several of my open source projects too, for example https://github.com/seancorfield/usermanager-example/blob/master/src/usermanager/main.clj#L212-L219

🙏 3
jamesleonis21:08:28

I also generally have a (comment ...) block at the bottom of my files. It's often my scratchpad for building up functions and implementations. Sometimes I migrate them into tests. https://gitlab.com/jamesleonis/dht-cljc/-/blob/master/src/dht_cljc/infohash.cljc#L58

🙏 3