What does * allow-unresolved-vars * do in Clojure?

As there is no official document for it http://clojuredocs.org/clojure_core/clojure.core/ allow-unresolved-vars

+5
source share
1 answer

Apologies are not the full answer. I haven't figured it all out yet.


*allow-unresolved-vars* is defined in RT.java :

 final static Var ALLOW_UNRESOLVED_VARS = Var.intern(CLOJURE_NS, Symbol.intern("*allow-unresolved-vars*"), F).setDynamic(); 

and used in Compiler.java :

  if(o == null) { if(RT.booleanCast(RT.ALLOW_UNRESOLVED_VARS.deref())) { return sym; } else { throw Util.runtimeException("Unable to resolve symbol: " + sym + " in this context"); } } 

Thus, its use here is to decide whether an exception should be thrown immediately when an unresolved symbol occurs.

You can handle it:

 myns.core=> (ns clojure.core) nil clojure.core=> oops! CompilerException java.lang.RuntimeException: Unable to resolve symbol: oops! in this context, compiling:(/tmp/form-init1596111142512149454.clj:1:884) clojure.core=> (defn q [] (oops!)) CompilerException java.lang.RuntimeException: Unable to resolve symbol: oops! in this context, compiling:(/tmp/form-init1596111142512149454.clj:1:12) clojure.core=> (def *allow-unresolved-vars* true) Warning: *allow-unresolved-vars* not declared dynamic and thus is not dynamically rebindable, but its name suggests otherwise. Please either indicate ^:dynamic *allow-unresolved-vars* or change the name. (/tmp/form-init1596111142512149454.clj:1) #'clojure.core/*allow-unresolved-vars* clojure.core=> oops! IllegalArgumentException UnresolvedVarExpr cannot be evalled clojure.lang.Compiler$UnresolvedVarExpr.eval (Compiler.java:1771) clojure.core=> clojure.core=> (defn q [] (oops!)) CompilerException java.lang.VerifyError: (class: clojure/core$q, method: invoke signature: ()Ljava/lang/Object;) Unable to pop operand off an empty stack, compiling:(form-init1596111142512149454.clj:1:1) 

But I still haven't figured out how to use it, since unresolved variables still cause errors - these are just different errors. In addition, I do not understand the warning when it is not resolved, because the warning says that it is not declared dynamic, while it looks like it is declared dynamic in RT.java.

+1
source

Source: https://habr.com/ru/post/1200369/


All Articles