If by 'constant' you mean something stored in a global var
(def my-nullable nil)
that can be resolved at compile-time, then:
(defmacro compile-time-var-nil-check [x]
(let [x @(resolve x)]
(assert (not (nil? x)))))
Question
We can see in Clojure a way to use design by contract to check that arguments to a function in Clojure are not-nil at runtime.
(defn constrained-fn [ x]
{:pre [(not (nil? x))]}
x)
(constrained-fn nil)
=> AssertionError Assert failed: (not (nil? x)) ...../constrained-fn (form-init5503436370123861447.clj:1)
We can see a way to check the type of an expression at compile time in Clojure
(defmacro typeof
([expression]
(:class (expression-info expression)))
(defmacro primitive?
([expression]
(:primitive? (expression-info expression))))
Here is my take on the question based on compile-time macros in Clojure:
(defmacro compile-time-nil-check [x] (assert (not (nil? x))))
(def my-nullable nil)
(compile-time-nil-check my-nullable)
My question is - is there a way to check that a 'constant' is not nil at compile time in Clojure?
(I realise this question might seem trivial- I want to use it as a building block for a subsequent question about compile-time type checks in Clojure).
Solution
If by 'constant' you mean something stored in a global var
(def my-nullable nil)
that can be resolved at compile-time, then:
(defmacro compile-time-var-nil-check [x]
(let [x @(resolve x)]
(assert (not (nil? x)))))