summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2021-12-23 07:59:18 -0800
committerKaz Kylheku <kaz@kylheku.com>2021-12-23 07:59:18 -0800
commitd1caae1ac6f393d0bc8cbcf62804dbac0033d133 (patch)
treecbc49bc073c8086c1f97f1efb5378eb518484896 /tests
parentda6829688c5ff6d294cb6157a84166837c880562 (diff)
downloadtxr-d1caae1ac6f393d0bc8cbcf62804dbac0033d133.tar.gz
txr-d1caae1ac6f393d0bc8cbcf62804dbac0033d133.tar.bz2
txr-d1caae1ac6f393d0bc8cbcf62804dbac0033d133.zip
new feature: :mass-delegate struct clause macro.
With :mass-delegate, it is possible to generate delegation methods in bulk. All of the methods of a struct type can be mirrored by delegates in another struct type just by writing a single :mass-delegate clause. * stdlib/struct.tlk (:mass-delegate): New struct clause macro. * tests/012/oop.tl: New tests. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'tests')
-rw-r--r--tests/012/oop.tl33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/012/oop.tl b/tests/012/oop.tl
index a5c57973..b786e0b7 100644
--- a/tests/012/oop.tl
+++ b/tests/012/oop.tl
@@ -108,3 +108,36 @@
(mtest co.(work) "worker foo works"
co.(break) "worker foo relaxes for 15 min"
co.(break 5) "worker foo relaxes for 5 min"))
+
+(defstruct api-x ()
+ (:method get (x a b : c . d) ^(api-x get ,x ,a ,b ,c ,d))
+ (:method put (x s) ^(api-x put ,x ,s)))
+
+(defstruct api-y ()
+ (:method frob (y r : s) ^(api-y frob ,y ,r ,s))
+ (:method tweak (y) ^(api-y tweak ,y)))
+
+(defstruct api-z ()
+ (:method decrement (z n) ^(api-z decrement ,z ,n))
+ (:method increment (z n) ^(api-z increment ,z ,n)))
+
+(defstruct component ()
+ (ax (new api-x))
+ (ay (new api-y))
+ (az (new api-z))
+ (:mass-delegate o o.ax api-x *)
+ (:mass-delegate o o.ay api-y frob)
+ (:mass-delegate o o.az api-z * decrement))
+
+(let ((c (new component)))
+ (mtest
+ c.(get 1 2 3 . 4) (api-x get #S(api-x) 1 2 3 4)
+ c.(put 5) (api-x put #S(api-x) 5)
+ c.(get) :error
+ c.(put 5 6) :error
+ c.(frob 7 8) (api-y frob #S(api-y) 7 8)
+ c.(frob 9) (api-y frob #S(api-y) 9 nil)
+ c.(frob 7 8 9) :error
+ c.(tweak) :error
+ c.(increment 1) (api-z increment #S(api-z) 1)
+ c.(decrement) :error))