From: Lars Brinkhoff <lars@nocrew.org>
To: emacs-devel@gnu.org
Subject: Re: RFC: User-defined pseudovectors
Date: Thu, 10 Oct 2013 13:40:44 +0200 [thread overview]
Message-ID: <85bo2xcq0z.fsf@junk.nocrew.org> (raw)
In-Reply-To: 85fvs9cqjo.fsf@junk.nocrew.org
Just a small demo of how it would be possible to change cl-defstruct.
With the previous patch applied, plus this one, we get this:
(cl-defstruct foo x y z)
=> foo
(let ((x (make-foo :y 1)))
(list (type-of x)
(foo-p x)
(typed-pseudovector-p x)
(foo-y x)
x))
=> (foo t t 1 #%[foo nil 1 nil])
diff a/lisp/emacs-lisp/cl-macs.el b/lisp/emacs-lisp/cl-macs.el
--- a/lisp/emacs-lisp/cl-macs.el
+++ b/lisp/emacs-lisp/cl-macs.el
@@ -2312,6 +2312,12 @@ Like `cl-callf', but PLACE is the second argument of FUNC, not the first.
;;; Structures.
+(defun typed-pseudovector (type &rest elements)
+ (let ((result (make-typed-pseudovector (length elements) type nil))
+ (i 0))
+ (dolist (elt elements result)
+ (aset result (cl-incf i) elt))))
+
;;;###autoload
(defmacro cl-defstruct (struct &rest descs)
"Define a struct type.
@@ -2450,21 +2456,25 @@ non-nil value, that slot cannot be set via `setf'.
(or (memq type '(vector list))
(error "Invalid :type specifier: %s" type))
(if named (setq tag name)))
- (setq type 'vector named 'true)))
+ (setq type 'typed-pseudovector named 'true tag name)))
(or named (setq descs (delq (assq 'cl-tag-slot descs) descs)))
(push `(defvar ,tag-symbol) forms)
(setq pred-form (and named
(let ((pos (- (length descs)
(length (memq (assq 'cl-tag-slot descs)
descs)))))
- (if (eq type 'vector)
- `(and (vectorp cl-x)
- (>= (length cl-x) ,(length descs))
- (memq (aref cl-x ,pos) ,tag-symbol))
+ (cond
+ ((eq type 'vector)
+ `(and (vectorp cl-x)
+ (>= (length cl-x) ,(length descs))
+ (memq (aref cl-x ,pos) ,tag-symbol)))
+ ((eq type 'list)
(if (= pos 0)
`(memq (car-safe cl-x) ,tag-symbol)
`(and (consp cl-x)
- (memq (nth ,pos cl-x) ,tag-symbol))))))
+ (memq (nth ,pos cl-x) ,tag-symbol))))
+ (t
+ `(memq (type-of cl-x) ,tag-symbol)))))
pred-check (and pred-form (> safety 0)
(if (and (eq (cl-caadr pred-form) 'vectorp)
(= safety 1))
@@ -2488,9 +2498,10 @@ non-nil value, that slot cannot be set via `setf'.
(list `(or ,pred-check
(error "%s accessing a non-%s"
',accessor ',name))))
- ,(if (eq type 'vector) `(aref cl-x ,pos)
- (if (= pos 0) '(car cl-x)
- `(nth ,pos cl-x)))) forms)
+ ,(if (eq type 'list)
+ (if (= pos 0) '(car cl-x)
+ `(nth ,pos cl-x))
+ `(aref cl-x ,pos))) forms)
(push (cons accessor t) side-eff)
(if (cadr (memq :read-only (cddr desc)))
(push `(gv-define-expander ,accessor
next prev parent reply other threads:[~2013-10-10 11:40 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-10 11:22 RFC: User-defined pseudovectors Lars Brinkhoff
2013-10-10 11:29 ` Lars Brinkhoff
2013-10-10 11:40 ` Lars Brinkhoff [this message]
2013-10-10 12:52 ` Dmitry Antipov
2013-10-10 13:41 ` Dmitry Antipov
2013-10-10 16:40 ` Lars Brinkhoff
2013-10-10 14:00 ` Stefan Monnier
2013-10-10 16:30 ` Lars Brinkhoff
2013-10-10 20:42 ` Stefan Monnier
2013-10-11 6:00 ` Lars Brinkhoff
2013-10-11 12:22 ` Stefan Monnier
2013-10-12 16:01 ` User-defined record types Lars Brinkhoff
2013-10-12 18:58 ` Stefan Monnier
2013-10-18 13:39 ` Ted Zlatanov
2013-10-18 15:28 ` Stefan Monnier
2013-10-18 23:24 ` Ted Zlatanov
2013-10-19 2:09 ` Stefan Monnier
2013-10-19 2:30 ` Drew Adams
2013-10-19 11:48 ` Ted Zlatanov
2013-10-19 14:37 ` Stefan Monnier
2013-10-19 20:11 ` Ted Zlatanov
2013-10-19 21:48 ` Stefan Monnier
2013-10-10 20:43 ` RFC: User-defined pseudovectors Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=85bo2xcq0z.fsf@junk.nocrew.org \
--to=lars@nocrew.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).