Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I type-hint a Vector of Strings in Clojure?

Tags:

types

clojure

My function returns a sequence, for example Vector of Strings.

Here is a trivial example (which in practice would be derived from type inference, but which illustrates the point):

(defn ^PersistentVector myfunction [a b] 
    ;; do something with strings
)

(my-function  ["A" "B"])

How do I type-hint this to show that these are specifically Strings?

Something like ^PersistentVector<String>?

like image 681
Joshua Fox Avatar asked Dec 06 '25 10:12

Joshua Fox


1 Answers

A PersistentVector can contain objects of any type, there is no way to enforce the type of the content, so a notation for such a type hint does not exist. You can have it return a Java array of strings and then you can use the convenience type hint (defn ^"[Ljava.lang.String;" function [a b]):

(defn ^"[Ljava.lang.String;" function [a b]
  (into-array String [a b]))

(type (function "a" "b"))
;; => [Ljava.lang.String;

like image 137
Erwin Rooijakkers Avatar answered Dec 08 '25 23:12

Erwin Rooijakkers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!