user3139545
user3139545

Reputation: 7374

Creating records using macro in Clojure

I want to define a vector with all the values i want to use in my record and then pass this vector to a macro that creates my record.

(def keys ['data1 'data2 'data3])

(defmacro make-record  [n v & body] `(defrecord ~n ~v ~@body))
(make-record VUD vud-keys)
(macroexpand-1 '(make-record TYPE keys)) -> (defrecord TYPE keys)

What is want is:

(macroexpand-1 '(make-record TYPE keys)) -> (defrecord TYPE ['data1 'data2 'data3])

Upvotes: 1

Views: 98

Answers (1)

coredump
coredump

Reputation: 38809

It looks like you need to evaluate your arguments during macroexpansion. This is what eval does.

(def keys '[data1 data2 data3])
(defmacro make-record [name keys]
  `(defrecord ~name ~(eval keys)))

Upvotes: 2

Related Questions