1
votes

New to clojure and i'm wondering.. can i define a var as both ^:private and ^:const?

So for instance, would it make sense to do the following:

(def ^:private ^:const everything 42)
1
why not? technically would be both in metadata (see (meta #'everything)), semantically these metas have different meanings, that don't interleave. :const would be an optimization helper, while :private is obviously controls visibility - leetwinski
Unfortunately, both of these are imperfect and can provide "false assurance" to the user. I never use either one. Please see stackoverflow.com/questions/9162558/how-does-clojure-const-work - Alan Thompson

1 Answers

1
votes

Yes, on any sufficiently modern Clojure version.

long ago the metadata keywords didn't "roll up" but that was fixed years ago.

the ^:keyword reader-macro is just a syntactic shorthand for ^{:keyword true}

user> (def ^:private ^:const everything 42)
#'user/everything
user> (meta #'everything)
{:const true, :private true, :line 241457, :column 7, :file "*cider-repl api*", :name everything, :ns #namespace[user]}

is the same as:

user> (def ^{:private true :const true} everything 42)
#'user/everything
user> (meta #'everything)
{:private true, :const true, :line 241816, :column 7, :file "*cider-repl api*", :name everything, :ns #namespace[user]}