如何在 Clojure 中对字符串向量进行类型提示?

发布于 2025-01-21 04:03:11 字数 286 浏览 2 评论 0原文

我的函数返回一个序列,例如字符串向量。

这是一个微不足道的示例(实际上,它是从类型推理中得出的,但说明了这一点):

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

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

我该如何键入此提示以表明这些是专门的字符串?

类似^persistentVector< string>

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>?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神妖 2025-01-28 04:03:11

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文