🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.clos
Article #304

Re: class variables

#304
From: Barry Margolin
Date: Thu, 01 Oct 1992 14:50
60 lines
3032 bytes
In article <1992Oct1.071605.8674@cs.ucla.edu> lange@lanai.cs.ucla.edu (Trent Lange) writes:
>I have to agree with this.  Though the current :allocation :class option
>is certainly more useful than defvar (as Barry points out), it would be
>*very* useful if there was an slot :allocation option that allocated
>:class-like slots per subclass -- maybe an option called :class-class
>or something.  

I'd call it something like :PER-CLASS.

>It would definitely add functionality to CLOS, and in a very intuitive way,
>so that (setf (slot-value (class-prototype classA) 'class-class-slot) X)
>would set the slot-values of all instances of classA to X, without
>changing the slot-values of instances of other classes.

I don't think it really adds that much functionality.  It's relatively
straightforward to implement using existing CLOS facilities, not even MOP.

(defclass per-class-slot-mixin ()
  (value-table :allocation :class :initform '() :accessor value-table))

(defmethod per-class-slot-value ((self per-class-slot-mixin) (name symbol))
  (let* ((class-table (assoc (class-of self) (value-table self)))
	 (value-entry (assoc name (cdr class-table))))
    (if value-entry
	(cdr value-entry)
	(error ...))))

(defmethod (setf per-class-slot-value) (new-value (self per-class-slot-mixin) (name symbol))
  (let ((class-table (assoc (class-of self) (value-table self))))
    (unless class-table
      (setf class-table (cons (class-of self) nil))
      (push class-table (value-table self)))
    (let ((value-entry (assoc name (cdr class-table))))
      (if value-entry
	  (setf (cdr value-entry) new-value)
          (push (cons name new-value) (cdr value-entry))))))

By using MOP you should be able to make this accessible using ordinary
SLOT-VALUE.  But since most slot access should be done via accessor
methods, I don't really see the need for this, as use of
PER-CLASS-SLOT-VALUE could be hidden inside the accessors:

(defmethod foo-slot ((self foo))
  (per-class-slot-value self 'slot))

(defmethod (sef foo-slot) (new-value (self foo))
  (setf (per-class-slot-value self 'slot) new-value))

If you find this tedious, hide it in a macro.

>Is this the kind of thing that might be put up for review in the proposed
>dpANS Common Lisp standard?

You can suggest anything you want in your review comments, and this is
certainly the kind of thing that's appropriate.
-- 
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar
Technical Details
Message-ID: <1af37aINN232@early-bird.think.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!spool.mu.edu!think.com!barmar
References: <HALTRAET.92Sep30032338@monsun.si.no> <CUTTING.92Sep30102223@skye.parc.xerox.com> <1992Oct1.071605.8674@cs.ucla.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!caen!spool.mu.edu!think.com!barmar
From: barmar@think.com (Barry Margolin)
Newsgroups: comp.lang.clos
Subject: Re: class variables
Date: 1 Oct 1992 14:50:18 GMT
Organization: Thinking Machines Corporation, Cambridge MA, USA
Lines: 60
Message-ID: <1af37aINN232@early-bird.think.com>
References: <HALTRAET.92Sep30032338@monsun.si.no> <CUTTING.92Sep30102223@skye.parc.xerox.com> <1992Oct1.071605.8674@cs.ucla.edu>
NNTP-Posting-Host: telecaster.think.com