Skip to content

Commit 056ba65

Browse files
Merge pull request #18 from active-group/omit-field-names-from-constructor
Record: Allow to omit field names from constructor spec
2 parents ceac622 + deebe93 commit 056ba65

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/active/clojure/record.cljc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@
6464
(defmacro define-record-type
6565
"Attach doc properties to the type and the field names to get reasonable docstrings."
6666
[?type ?constructor-call ?predicate ?field-specs & ?opt+specs]
67-
(when-not (and (list? ?constructor-call)
68-
(not (empty? ?constructor-call)))
67+
(when-not (or (and (list? ?constructor-call)
68+
(not (empty? ?constructor-call)))
69+
(symbol? ?constructor-call))
6970
(throw (throw-illegal-argument-exception (str "constructor call must be a list in " *ns* " " (meta &form)))))
7071
(when-not (vector? ?field-specs)
7172
(throw (throw-illegal-argument-exception (str "field specs must be a vector in " *ns* " " (meta &form)))))
@@ -100,8 +101,13 @@
100101
:else
101102
(throw (throw-illegal-argument-exception (str "invalid field spec " spec " in " *ns* " " (meta &form))))))))
102103

103-
?constructor (first ?constructor-call)
104-
?constructor-args (rest ?constructor-call)
104+
[?constructor & ?constructor-args] (cond
105+
(list? ?constructor-call)
106+
?constructor-call
107+
108+
(symbol? ?constructor-call)
109+
(concat [?constructor-call]
110+
(map first ?field-triples)))
105111
?constructor-args-set (set ?constructor-args)
106112
document (fn [n doc]
107113
(vary-meta n

test/active/clojure/record_test.cljc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,24 @@
3232
(is (thrown? Throwable
3333
(kar (FakePare. 1 2)))))
3434

35+
36+
;; Omit constructor args
37+
38+
(define-record-type Schmare
39+
schmons
40+
schmare?
41+
[a schmar
42+
b schmdr])
43+
44+
(deftest simple-omit
45+
(let [r (schmons 1 2)]
46+
(is (schmare? r))
47+
(is (= 1 (schmar r)))
48+
(is (= 2 (schmdr r)))))
49+
50+
51+
;; Uninstantiated fields
52+
3553
(define-record-type Pu
3654
(make-pu c a)
3755
pu?

0 commit comments

Comments
 (0)