20110224

NEW structure and class constructor handling?

In a previous post I mused about having a new new operator for Common Lisp.

The only rub in the scheme I came up with was the handling of DEFSTRUCT constructors, which had to be somehow recorded in the system in order to make them known to the new operator.

The solution was less than satisfactory, and I had been thinking about how to come up with something more pleasant. Now I believe I found something acceptable by relaxing the basic new invariant (cfr., the spec) and admitting two "pseudo" types as arguments. Suppose we had a "pseudo" type (class class-name) and a "pseudo" type (struct structure-class-name). Now I could write:

cl-prompt> (defclass foo () ())
#<STANDARD CLASS FOO>

cl-prompt> (newq (class foo))
#<FOO @ 42>
The twist is that now I have the right structure to plug in the constructor for structures.
cl-prompt> (defstruct baz q w e)
BAZ

cl-prompt> (newq (struct baz) :q 42)
#S(BAZ :Q 42 :W NIL :E NIL) ; The standard MAKE-BAZ constructor is called.

cl-prompt> (defstruct (bar (:constructor barify (a s &optional d)
                           (:constructor new-bar))
               a s d)
BAR

cl-prompt> (newq (struct bar :by barify) 1 2)
#S(BAR :A 1 :S 2 :D NIL)

cl-prompt> (newq (struct bar :by new-bar) :s 2 :a 1)
#S(BAR :A 1 :S 2 :D NIL)

I think this does it and it is not so bad. Any comments?


(cheers)

2 comments:

  1. I liked the old syntax better -- it was more succinct. Are you saying this is an alternate syntax, or the old syntax is being replaced?

    Since I never use the :constructor option this change doesn't address my issues, which is, primarily, cleaner code.

    But I would get on board if it were optional. I prefer designing for the most common 80% and not let the other 20% unduly influence the common use cases.
    Just my 2 cents.

    ReplyDelete
  2. It would definitively be optional. (CLASS c) is introduced only for symmetry with (STRUCT s). You could still use the constructor recording machinery and use (NEW structure-class ...) syntax.

    Apart from that, there are uses for :constructor that are very useful. I'll blog about it.

    (cheers)

    ReplyDelete