How to make a record from a sequence of values

后端 未结 5 1316
执笔经年
执笔经年 2020-12-20 13:38

I have a simple record definition, for example

(defrecord User [name email place])

What is the best way to make a record having it\'s value

5条回答
  •  孤城傲影
    2020-12-20 14:08

    the defrecord function creates a compiled class with some immutable fields in it. it's not a proper clojure functions (ie: not a class that implements iFn). If you want to call it's constructor with apply (which expects an iFun) you need to wrap it in an anonymous function so apply will be able to digest it.

    (apply #(User. %1 %2 %3 %4) my-values)

    it's closer to what you started with though your approach of defining a constructor with a good descriptive name has its own charm :)

    from the API:

    Note that method bodies are
    not closures, the local environment includes only the named fields,
    and those fields can be accessed directy.

提交回复
热议问题