4 ms·
I do this in Scala, via https://www.scala-lang.org/api/2.12.13/scala/util/DynamicVariable.html https://www.scala-lang.org/api/2.12.13/scala/util/DynamicVar...
by chriswarbo 4y ago
I do this in Scala, via https://www.scala-lang.org/api/2.12.13/scala/util/DynamicVariable.html https://www.scala-lang.org/api/2.12.13/scala/util/DynamicVar...
It's not perfect, since it uses JVM's thread-local storage under the hood; this can break when e.g. evaluating Futures in a ThreadPool. For variables which are rarely-overridden, like loggers, I do so with a wrapper that also switches the ExecutionContext to a new ThreadPool (urgh, multithreading...)
PS: I do the same for env vars too ;) http://www.chriswarbo.net/blog/2021-04-08-env_vars.html http://www.chriswarbo.net/blog/2021-04-08-env_vars.html
- TeMPOraL 4y agoThat's a neat post, thanks for sharing! I didn't realize Scala had dynamic variables. As for the HN comment that prompted your blog article, I did a double-take reading it, because I could've sworn I wrote the exact same thing around the same time - turns out I did, though on a different thread :).