Skip to content

Commit a0ff84e

Browse files
authored
Fix deftype/defrecord constructors #1025 (#1026)
Fixes #1025
1 parent 488fa5b commit a0ff84e

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
### Fixed
99
* Fix a bug where the compiler would always generate inline function definitions even if the `inline-functions` compiler option is disabled (#1023)
10+
* Fix a bug where `defrecord`/`deftype` constructors could not be used in the type's methods. (#1025)
1011

1112
## [v0.2.1]
1213
### Changed

src/basilisp/core.lpy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6592,6 +6592,7 @@
65926592

65936593
{:keys [interfaces methods]} (collect-methods method-impls)]
65946594
`(do
6595+
(declare ~type-name ~ctor-name)
65956596
(deftype* ~type-name ~fields
65966597
:implements [~@interfaces
65976598
~'basilisp.lang.interfaces/IType
@@ -6765,6 +6766,7 @@
67656766
(validate-record-methods methods)
67666767

67676768
`(do
6769+
(declare ~type-name ~ctor-name ~map-ctor)
67686770
(deftype* ~type-name ~record-fields
67696771
:implements [~@interfaces]
67706772

tests/basilisp/test_defrecord.lpy

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
11
(ns tests.basilisp.test-defrecord
22
(:import abc)
33
(:require
4-
[basilisp.test :refer [deftest is testing]]))
4+
[basilisp.test :refer [deftest is testing are]]))
55

66
(deftype Square [dim]
77
(__eq__ [this other]
88
(and (instance? Square other)
99
(= dim (.-dim other)))))
1010

11+
(deftype InvokeConstructorsType []
12+
(__call__ [this ctor]
13+
(case ctor
14+
:positional (->InvokeConstructorsType)
15+
:dot (InvokeConstructorsType.))))
16+
1117
(deftest deftype-constructors
1218
(let [elems [(tests.basilisp.test-defrecord.Square. 1)
1319
(Square. 1)
1420
(new tests.basilisp.test-defrecord.Square 1)
1521
(new Square 1)
1622
(tests.basilisp.test-defrecord/Square 1)]]
17-
(is (apply = elems))))
23+
(is (apply = elems)))
24+
(testing "can invoke own constructors"
25+
(let [invoke (->InvokeConstructorsType)]
26+
(are [k] (instance? InvokeConstructorsType (invoke k))
27+
:positional
28+
:dot))))
1829

1930
(deftest deftype-reader-form
2031
(testing "type"
@@ -184,13 +195,26 @@
184195
(area [self]
185196
(* 3.14 radius radius)))
186197

198+
(defrecord InvokeConstructorsRecord []
199+
(__call__ [self ctor]
200+
(case ctor
201+
:map (map->InvokeConstructorsRecord self)
202+
:positional (->InvokeConstructorsRecord)
203+
:dot (InvokeConstructorsRecord.))))
204+
187205
(deftest defrecord-constructors
188206
(let [elems [(tests.basilisp.test-defrecord.Circle. 1)
189207
(Circle. 1)
190208
(new tests.basilisp.test-defrecord.Circle 1)
191209
(new Circle 1)
192210
(tests.basilisp.test-defrecord/Circle 1)]]
193-
(is (apply = elems))))
211+
(is (apply = elems)))
212+
(testing "can invoke own constructors"
213+
(let [invoke (->InvokeConstructorsRecord)]
214+
(are [k] (instance? InvokeConstructorsRecord (invoke k))
215+
:map
216+
:positional
217+
:dot))))
194218

195219
(deftest defrecord-with-methods
196220
(let [c (->Circle 1)

0 commit comments

Comments
 (0)