https://caveman.mccue.dev/tutorial/clojure/27_run_tests_against_your_database
In the above tutorial,I found that (PostgreSQLContainer/.getPassword container) is used instead of just (.getPassword container) . Is there any practical reason to use (ClassName/.someInstanceMethod obj) syntax?
In that context, it's needed to avoid reflection (as indicated by the warnings you'll get when loading that code without PostgreSQLContainer/, due to the (set! *warn-on-reflection* true) form above) that would happen because container doesn't have a type known to the compiler.
However, you can still use (.getPassword ...) and have no reflection if you type-hint container. It can either be done at the binding site - (let [^PostgreSQLContainer container ...] ...) - or in the function that makes the container:
(defn start-pg-test-container
^PostgreSQLContainer []
...)
I'd say the latter is better because then every start-pg-test-container call site will have the type info.Also, FWIW I would probably use .getJdbcUrl and add username and password to it instead of constructing the whole URL myself.
Something like this
(defn start-pg-test-container
^PostgreSQLContainer []
(let [container (PostgreSQLContainer.
(-> (DockerImageName/parse "postgres")
(.withTag "17")))]
(doto container
(.withUrlParam "user" (.getUsername container))
(.withUrlParam "password" (.getPassword container))
(.start)
(.waitingFor (Wait/forListeningPort)))))
And with just :jdbUrl (.getJdbcUrl container) in get-test-db.In my case stuff like this often ends up in the explicit form just because I initially used it for auto-completing the method I was after...
At least Cursive can with reasonable reliability offer sensible auto-completion even when there's nothing else in the form. I guess based on the context, I dunno.
p-himik, Thanks! I was asking ChatGPT and it can't help much. just keep answering that (.someInstanceMethod obj ...) is the correct way.
Yes, I just checked and it seems Cursive can still show methods available for auto-completion.
I think emacs (cider) also can do that if you start with the object already stated e.g. (.<cursor> object), but I guess I'm not into that habbit yet.
aside, is (Foo/.bar o) a new syntax, or is it just something I don't remember seeing?
New in 1.12. And there are some other new things.