在Clojure中获取函数的参数类型提示 [英] Getting argument type hints of a function in Clojure

查看:70
本文介绍了在Clojure中获取函数的参数类型提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找提取函数自变量的类型提示信息,但似乎找不到找到该信息的方法.

I'm looking to extract the type hint information of a function's arguments, but I can't seem to find a way to access that information.

例如,说我具有以下功能:

For example, say I have the following function:

(defn ^Double do-something [^String a, ^String b]
  5.0)

拉标签很简单:

(:tag (meta #'do-something)) ; => java.lang.Double

但是,对于参数,这样的操作将不起作用:

For the arguments, however, something like this won't work:

(:arglists (meta #'do-something)) ; => ([a b])

这只是给我参数而不是类型信息.有没有办法获取ab的类型?

This just gives me the arguments and not the type information. Is there a way to get the type of a and b?

之所以要这样做,是因为我正在编写一个用于分析/记录函数的工具,并且如果函数是类型提示的,我想知道这一点.

The reason I want to do this is because I'm writing a tool to analyze/document functions, and if a function is type hinted, I'd like to know that.

仅出于文档目的向代码添加类型提示似乎不是一个特别好的主意,但我只想使用该信息(如果该信息已经存在,并且不存在其他类型信息的话)(例如好像使用了core.typed).

Adding type hints to code for the sole purpose of documentation seems like not a particularly great idea, but I'd just like to use the information if it's already there in the first place and if no other type information is present (such as perhaps if core.typed was used).

推荐答案

您需要获取参数的元数据:

You need to get the metadata of the arguments:

user=> (map meta (first (:arglists (meta #'do-something))))
({:tag String} {:tag String})

这篇关于在Clojure中获取函数的参数类型提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆