aboutsummaryrefslogtreecommitdiff
path: root/_pastebins/2023-07-22-funcallable-amop.md
blob: 37c79fe5316ad32e2d7642f0f26b76b258b64f17 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
---

title: Funcallable AMOP

date: 2023-07-22

layout: post

lang: en

ref: funcallable-amop

---


Using `macrolet` to allow a `funcallable-standard-class` to be invoked without
using `funcall` directly, and let the macroexpansion do that instead:

```
#!/usr/bin/env li

(asdf:load-system :closer-mop)

(defclass constructor ()
  ((name   :initarg :name   :accessor constructor-name))
  (:metaclass closer-mop:funcallable-standard-class))

(defmethod initialize-instance :after ((c constructor) &key)
  (with-slots (name) c
    (closer-mop:set-funcallable-instance-function
     c
     (lambda (x)
       (format t "~s: ~s - ~s~%" name :funcalled x)))))

(let ((c (make-instance 'constructor :name "the-name")))
  (funcall c 1))

(let ((c (make-instance 'constructor :name "the-name")))
  (macrolet ((c (&body body)
               `(funcall c ,@body)))
    (funcall c 2)
    (c 3)))
```