找到关于函数参数的类型 [英] find about about the type of function parameters

查看:149
本文介绍了找到关于函数参数的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如,什么函数接受一个命名空间对象作为参数?



(这是因为我可以创建一个命名空间并将其存储在一个var,但我不知道我可以使用该var;什么函数可能传递该var)



这里是一些代码:

  user => (def work-namespace(create-ns'my-namespace))
#'user / working-namespace
;我创建了一个namspace并希望以后使用

=> (class working-namespace)
clojure.lang.Namespace
;出于好奇,我发现工作命名空间是一个命名空间对象

user => (ns working-namespace)
nil
working-namespace =>
;但是如何切换到它?这没有做我想要的...

user => (参考working-namespace)
java.lang.ClassCastException:clojure.lang.Namespace不能转换为clojure.lang.Symbol(NO_SOURCE_FILE:0)
;这没有工作作为我的期望

user => (the-ns working-namespace)
#< Namespace my-namespace>
user => (class(the -ns working-namespace))
clojure.lang.Namespace
;伟大的,这给了我一样的东西,命名空间

因此的问题: (这就是为什么我把我的命名空间var)?我如何从指向一个命名空间的var获取对我有用的东西?



我可以尝试看看使用Namespace对象或转换它的函数到别的东西。我做了,只找到实习生。用手搜索似乎不那么有希望



如果我有这个问题一百万次怎么办?

解决方案

在Clojure 1.2中有一个自动化的方法来获取我正在寻找的东西,和以前的函数参数没有类型。 每个函数参数是一个对象。所以问题真的成为我如何找到函数,将投射对象我传递他们到这种类型,所以搜索类型提示会找到一些,虽然它不会得到你的一切,我希望它更有可能回答这个一般来说。



从1.3开始(当前开发分支9/2010)函数参数和返回类型可以有一个定义的类型,并将作为该类型传递/返回,而不是被丢弃的对象,然后投射在另一边。这从数值函数的exacution时间的一个零,其重要的限制,它仅适用于:静态函数和只有直接调用即:没有通过map / reduce / filter / etc。)没有很多发布这个变化,虽然它有重要的突变,整数不再被默认和整数(实际上Long)溢出抛出一个异常。可以阅读更多此处

 (defn ^:static fib ^ long [^ long n] 
(if(< = n 1)
1
因此,在1.3之后,就会出现这样的情况:



发布和广泛采用你会看到代码与更常见的定义类型,因为他们将提供一个大的速度利益,然后你将能够找到更多的函数通过参数类型,虽然仍然不是所有的。



在我学习函数参数类型的同一篇讲座中,Rich提到了Clojure远程Clojure未来的计划(在Clojure中的Clojure之后)更好地支持暴露编译器内部工具如IDEs。所以有希望有一天你会得到一个真正的答案这个问题。
动态语言使得这在实践中更难,在理论上更难。


can i somehow find all functions/macros that take a specific type of parameter ?

for example, what function accepts a Namespace object as parameter ?

(this is because i can create a namespace and store it in a var, but i don't know where i could use that var; what function might i pass that var to ?)

here is some code:

user=> (def working-namespace (create-ns 'my-namespace))
#'user/working-namespace
;i created a namspace and want to use it later

user=> (class working-namespace)
clojure.lang.Namespace
; out of curiosity i found out that "working-namespace" is a Namespace object

user=> (ns working-namespace)
nil
working-namespace=>
; but how do i switch to it ? this didn't do what i wanted...

user=> (refer working-namespace)
java.lang.ClassCastException: clojure.lang.Namespace cannot be cast to clojure.lang.Symbol (NO_SOURCE_FILE:0)
; this did not work either as my expectations

user=> (the-ns working-namespace)
#<Namespace my-namespace>
user=> (class (the-ns working-namespace))
clojure.lang.Namespace
; great, this gave me the same thing, a Namespace

hence the question: how do i use it dynamically (that's why i had put my namespace into a var) ? how do i get something useful for me from a var that points to a namespace ?

i can try look around for functions that make use of a Namespace object or that convert it to something else. i did and only found "intern". searching by hand not seems not that promising

what if i have this problem a million time ? is there an automated way to get me what i'm looking for without having to ask around each time ?

解决方案

In Clojure 1.2 and previous function arguments dont have types. every function argument is an object. So the question really becomes "how do i find functions that will cast the object I pass them into this type. so searching for type hints will find some of them, though it wont get you everything. I wish it where more possible to answer this in general.

starting with 1.3 (current dev branch 9/2010) function paramerters and return types can have a defined type and will be passed/returned as that type instead of being cast to object and then cast on the other side. This drops one of the zeros from the exacution time of numerical functions with the important limitation that it only works for :static functions and only with direct calls (ie: not through map/reduce/filter/etc.) There is not a lot published on this change yet though it has the important breaking change that integers are no longer boxed by default and integer (actually Long) overflow throws an exception. you can read more here

(defn ^:static fib ^long [^long n]
  (if (<= n 1)
    1
    (+ (fib (dec n)) (fib (- n 2)))))

so after 1.3 is released and widely adopted you will see code with more commonly defined types because they will offer a big speed benefit and then you will be able to find more functions by argument type though still not all of them.

At the same lecture where I learned about function argument types, Rich mentioned plans in the distant Clojure future (after 'Clojure in Clojure') about better support for exposing the compiler internals to tools such as IDEs. So there is hope that someday you will get a real answer to this question. Dynamic languages make this slightly more difficult in practice and a lot harder in theory.

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

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