What does the leading arrow in a name mean in clojure

前端 未结 2 1790
北恋
北恋 2021-02-02 11:42

Learning Clojure I came across code like below:

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


        
2条回答
  •  旧时难觅i
    2021-02-02 12:25

    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]))
    

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

    Compare that to:

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

    Which is syntactically invalid.

提交回复
热议问题