名字中的领先箭头是什么意思在clojure [英] What does the leading arrow in a name mean in clojure

查看:110
本文介绍了名字中的领先箭头是什么意思在clojure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

学习Clojure我遇到了如下代码:

Learning Clojure I came across code like below:

=> (defrecord Person [name, age])
user.Person
=> (->Person "john" 40)
#user.Person{:name "john", :age 40}
=> (Person. "tom" 30)
#user.Person{:name "tom", :age 30}


b $ b

问题是, - > Person 中的前导箭头(即 - > 是什么意思?这是一个读者宏或什么?我没有看到它在clojuredoc的阅读器部分的描述。此外, - >人 之间有什么区别?

the question is, what does the leading arrow(i.e., ->) in the ->Person mean ? It's a reader macro or what ? I see no description of it in the reader section of clojuredoc. Further, what is the difference between ->Person and Person. ?

推荐答案

它没有语法意义。它只是符号名称的一部分。在Lisps中,箭头 - > (或者甚至只是'>')通常用于暗示将一种类型转换或转换成另一种类型。在 defrecord 的宏扩展中:

It has no syntactic meaning. It is just part of the symbol name. In Lisps, the arrow -> (or even just '>') is often used to imply conversion of, or casting of, one type into another. In the macro expansion of the defrecord:

(macroexpand '(defrecord Person [name age]))

您可以看到它定义 - > ; Person 作为调用 Person 构造函数的函数。 - > Person (函数)可能比 Person更方便。 Java构造函数),因为你可以将它作为参数传递给其他函数,捕获它在一个变量并使用它等:

you can see that it defines ->Person as a function that calls the Person constructor. ->Person (the function) may be more convenient for you to use than Person. (the direct call to the Java constructor) as you can pass it as an argument to other functions, capture it in a variable and use it, etc:

(let [f ->Person]
  (f "Bob" 65))

比较:

(let [f Person.]
  (f "Bob" 65))

这在语法上无效。

这篇关于名字中的领先箭头是什么意思在clojure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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