Joshua Fox
Joshua Fox

Reputation: 19695

How do I type-hint a Vector of Strings in 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>?

Upvotes: 3

Views: 547

Answers (1)

user2609980
user2609980

Reputation: 10514

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;

Upvotes: 8

Related Questions