Commit 1ac93e10 authored by Marco Antoniotti's avatar Marco Antoniotti 💬
Browse files

Added new syntax to deal with structure

constructors.
parent ed9daeb4
......@@ -1005,5 +1005,64 @@ The defaults for each of the keyword arguments are implementation dependent.
finally (return ht))
))
;;;---------------------------------------------------------------------------
;;; New struct and class "syntax".
;;; In order to work around the struct contructor problem, I simply
;;; introduce a bit of syntax (think of 'typename' in C++).
;;; Now you can say:
;;;
;;; (defstruct (foo (:constructor lets-make-a-foo (a s &optional d)))
;;; a s d)
;;;
;;; (new '(structure foo :by lets-make-a-foo) 42 123)
;;;
;;; This will call the proper contructor.
;;;
;;; The same will work for '(class c)'. The full syntax is
;;;
;;; <struct-or-class-spec> ::=
;;; '(' <sc-keyword> <sc-type> [:by <cons-function>] ')'
;;; <sc-keyword> ::= structure | class
;;; <sc-type> ::= <a structure or class designator>
;;; <cons-function> ::= <a function designator>
(defmethod type-cons ((what (eql 'structure)) spec arguments)
(destructuring-bind (sc-type &optional by-kwd constructor)
spec
(when (and by-kwd (string-not-equal by-kwd "BY"))
(error "Unknown keyword ~A in ~S." (cons 'structure spec)))
(when (and by-kwd (null constructor))
(error "Missing constructor in ~S." (cons 'structure spec)))
(flet ((build-cons-name (structure-class-des)
(etypecase structure-class-des
(symbol (find-symbol (format nil "MAKE-~A"
structure-class-des)
(symbol-package
structure-class-des)))
))
)
(let ((constr (if by-kwd
constructor
(build-cons-name sc-type)))
)
(apply constr arguments)))))
(defmethod type-cons ((what (eql 'class)) spec arguments)
(destructuring-bind (sc-type &optional by-kwd constructor)
spec
(when (and by-kwd (string-not-equal by-kwd "BY"))
(error "Unknown keyword ~A in ~S." (cons 'structure spec)))
(when (and by-kwd (null constructor))
(error "Missing constructor in ~S." (cons 'structure spec)))
(let ((constr (if by-kwd
constructor
#'(lambda (&rest args)
(apply #'make-instance sc-type args))))
)
(apply constr arguments)))))
;;;; end of file -- new-operator.lisp --
......@@ -4,6 +4,7 @@
;;;; Please see file COPYING for copyright and licensing information.
(mk:defsystem "NEW-OP"
:author "Marco Antoniotti"
:components ((:file "new-op-pkg")
(:file "new-op"
:depends-on ("new-op-pkg"))))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment