objective-lisp/README.md

68 lines
2.1 KiB
Markdown
Raw Permalink Normal View History

2021-11-14 04:13:48 +00:00
# objective-lisp
2021-11-14 05:22:10 +00:00
Syntactic sugar for object-oriented Lisp.
`objective-lisp` provides a simple, concise, and (slightly) more conventional
2022-02-22 04:50:53 +00:00
syntax for accessing the slots and methods of objects. It defines a macro named
`O!`, and a reader macro for `#[...]` (although you can change these characters
in the code).
2021-11-14 05:22:10 +00:00
## Usage
2022-02-22 04:50:53 +00:00
**TL;DR:** `#[object (method args)]` is like `object.method(args)` in C++.
2021-11-14 05:22:10 +00:00
First, to enable `objective-lisp`'s syntax, just load the system:
``` common-lisp
(asdf:load-system 'objective-lisp)
```
`objective-lisp`'s syntax takes the form of a special S-expression, contained
2021-11-22 23:29:25 +00:00
in square brackets rather than parentheses. Each expression within acts upon the
result of the previous one, like a chain of `.` (dot) operators in C-like
languages.
2021-11-14 05:22:10 +00:00
```common-lisp
2022-02-22 04:50:53 +00:00
#[foo (bar) (baz) (quux)]
2021-11-22 23:29:25 +00:00
;; C++: foo.bar().baz().quux()
2021-11-14 05:22:10 +00:00
```
2021-11-22 23:29:25 +00:00
To call a method, just write it after the object:
2021-11-14 05:22:10 +00:00
``` common-lisp
2022-02-22 04:50:53 +00:00
#[object (method args...)]
2021-11-22 23:29:25 +00:00
;; => (method object args...)
2021-11-14 05:22:10 +00:00
```
Under the hood, this just passes `object` as the first argument to `method`, so
you can do stuff like this (I won't kinkshame you, but your coworkers might):
```common-lisp
2022-02-22 04:50:53 +00:00
#[object (slot-value 'slot-name) (setf value)]
;; => #[(slot-value object 'slot-name) (setf value)]
;; => (setf (slot-value object 'slot-name) value)
2021-11-14 05:22:10 +00:00
```
Slot accessors, and other methods that don't take additional arguments, can be
written without enclosing parentheses:
``` common-lisp
2022-02-22 04:50:53 +00:00
#[object get-something]
;; => (get-something object)
```
2021-11-22 23:29:25 +00:00
To access slots directly, use the `:slot` keyword:
2021-11-14 05:22:10 +00:00
``` common-lisp
2022-02-22 04:50:53 +00:00
#[object :slot slot-name]
2021-11-22 23:29:25 +00:00
;; => (slot-value object 'slot-name)
2021-11-16 16:57:40 +00:00
```
2022-02-22 04:50:53 +00:00
You can also just use the `O!` macro directly:
``` common-lisp
(O! object :slot foo (do-something args...))
;; => (do-something (slot-value object 'foo) args...)
```
2021-11-14 05:22:10 +00:00
## License
2021-11-22 23:29:25 +00:00
`objective-lisp` is public domain (CC0). You can do whatever you want with it. I
don't really care about credit, it's just a silly little thing I wrote in a few
hours. (And then rewrote just now because the syntax sucked.)
2021-11-14 05:22:10 +00:00
But if you find it useful, *please* let me know. I'd love to hear about it.