r/Clojure•
joinr

quick work-around for private vars (obscure)

Case: well-meaning library author decides to go the public/private route (rather than separating said private functions into namespaces while retaining opt-in visibility for future consumers), and consumer (me) needs to get the value of a declared private var (not function).

The typical work-around for functions is to use sharp-quote, #'the-private-function which is fine for functions but not so hot for vars with non-function values.

enter var-get:

(var-get #'the-private-var)
:some-hidden-value

I haven't run into this too often in practice (e.g. actually needing to scrape some data that's been privatized), but there's always a first.

edit: A more invasive method to just hope over to the namespace at run time and (def private-var private-var)

Posted Jun 26, 2019, 3:38 PM UTC · edited

53
andersmurphy•

Thanks for sharing, didn't know var-get existed.

Also you can still access the vars with sharp-quote you just need to deref it:

=> #'user/private-var

#'user/private-var

=> @#'user/private-var

"foo"
2
joinrOP•

nice, yet another one. did not know about derefing

1
didibus•

Ya I normally just use deref, pretty straightforward.

1