summaryrefslogtreecommitdiffstats
path: root/tests/012/oop.tl
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-12-22 21:29:13 -0800
committerKaz Kylheku <kaz@kylheku.com>2021-12-22 21:29:13 -0800
commit7eef2749ca3282585e65415712ebb810f2462a01 (patch)
tree80b2d4cbd8b13b256fee53895cf024663587bec9 /tests/012/oop.tl
parent85721a1b46f0718393b6344de7863a5a90214446 (diff)
downloadtxr-7eef2749ca3282585e65415712ebb810f2462a01.tar.gz
txr-7eef2749ca3282585e65415712ebb810f2462a01.tar.bz2
txr-7eef2749ca3282585e65415712ebb810f2462a01.zip
new feature: defstruct clause macros.
* lisplib.c (struct_set_entries): Trigger autoload on new symbols define-struct-clause and *struct-clause-expander*. * stdlib/struct.tl (*struct-clause-expander*): New variable. (defstruct): expand-slot local function now returns list of expanded slots, not a single slot; every case in the tree-case is converted to return a list. The syntax of a slot clause is first expanded through *struct-clause-expander hash; if that works then the resulting list is further scanned for expansions. (define-struct-clause): New macro. (:delegate): New struct clause defined with define-struct-clause. Provides single-slot delegation. * tests/012/oop.tl: Tests for :delegate. * txr.1: Documented define-struct-clause and :delegate. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'tests/012/oop.tl')
-rw-r--r--tests/012/oop.tl15
1 files changed, 15 insertions, 0 deletions
diff --git a/tests/012/oop.tl b/tests/012/oop.tl
index bab4ab68..a5c57973 100644
--- a/tests/012/oop.tl
+++ b/tests/012/oop.tl
@@ -93,3 +93,18 @@
(new* type a 3 b 4)) #S(ab a 3 b 4)
(let ((type (find-struct-type 'ab)))
(new* (type 3 4))) #S(ab a 3 b 4))
+
+(defstruct worker ()
+ name
+ (:method work (me) `worker @{me.name} works`)
+ (:method relax (me : (min 15)) `worker @{me.name} relaxes for @min min`))
+
+(defstruct contractor ()
+ sub
+ (:delegate work (me) me.sub.sub)
+ (:delegate break (me : min) me.sub.sub relax))
+
+(let ((co (new contractor sub (new contractor sub (new worker name "foo")))))
+ (mtest co.(work) "worker foo works"
+ co.(break) "worker foo relaxes for 15 min"
+ co.(break 5) "worker foo relaxes for 5 min"))