'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>?
Solution 1:[1]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cfrick |
