Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is [#^String str] in Clojure function type hint

Tags:

clojure

I saw a Clojure function like

(defn strFun
[#^String str]
(...))

I guess the #^String is type hint but what is the #and ^ before the String?

like image 293
ahala Avatar asked Jun 14 '26 10:06

ahala


1 Answers

It's the old format for specifying simple metadata tags before it was just ^TagName. You will see it in code written during this transition from time to time, though there is no need to use it.

user> #^String ["hello"]                
["hello"]                               
user> (meta #^String ["hello"])         
{:tag java.lang.String}                 

is the same as not using the #

user> (meta ^String ["hello"])          
{:tag java.lang.String}                 
user> 

PS: in this example I tagged a vector with the tag java.lang.String. This point to note is that the symbol you use as a tag will be resolved and the value that symbol resolves to will be used as the tag. So you can't use an undefined symbol.

like image 178
Arthur Ulfeldt Avatar answered Jun 16 '26 20:06

Arthur Ulfeldt