While writing a little toy program, I discovered I have no idea how to
make the value of one slot depend on the value of another slot when
initializing an object. Suppose I have a board class with two slots:
size and grid. The grid slot is a vector whose length depends on the
size argument. I accomplished this by defining a shared-initialize
:after method to initialize the grid:
(defclass board ()
((size :accessor size
:type fixnum
:initarg :size
:initform +default-grid-size+)
(grid :accessor grid
:type vector)))
(defmethod shared-initialize :after ((board board) (slots t) &rest initargs)
(declare (ignore initargs))
(with-slots (size grid) board
(setf grid (make-array (square size) :initial-element 'empty))))
It works, i.e., (make-instance 'board :size 3) works as I expect, but
is this the right way? Thanks in advance!
--
Regards, (concatenate
Constantine Vetoshev 'string (mapcar #'character
(reverse '(109 111 99 46 111 111 104
97 121 64 118 99 100 114 97 112 101 103))))
Constantine Vetoshev wrote:
> While writing a little toy program, I discovered I have no idea how to
> make the value of one slot depend on the value of another slot when
> initializing an object. Suppose I have a board class with two slots:
> size and grid. The grid slot is a vector whose length depends on the
> size argument. I accomplished this by defining a shared-initialize
> :after method to initialize the grid:
>
> (defclass board ()
> ((size :accessor size
> :type fixnum
> :initarg :size
> :initform +default-grid-size+)
> (grid :accessor grid
> :type vector)))
>
> (defmethod shared-initialize :after ((board board) (slots t) &rest initargs)
> (declare (ignore initargs))
> (with-slots (size grid) board
> (setf grid (make-array (square size) :initial-element 'empty))))
>
> It works, i.e., (make-instance 'board :size 3) works as I expect, but
> is this the right way? Thanks in advance!
Yep, though you may want instead to use an :after method on
initialize-instance. shared-initialize also runs during the several ways
the instance can be re-initialized, such as when a class gets modified
while an instance exists. This could happen during development half way
thru a game, when you decide to make some incremental adjustment.
Methinks this after method on shared-init will zap your game.
I use an :after on initialize-instance unless I have a reason to use
shared-initialize.
kt
--
http://tilton-technology.com
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Your Project Here! http://alu.cliki.net/Industry%20Application
(message (Hello 'Constantine)
(you :wrote :on '(Sat, 20 Mar 2004 22:36:52 GMT))
(
CV> (concatenate 'string (mapcar
CV> #'character (reverse '(109 111
CV> 99 46 111 111 104 97 121 64 118 99 100
CV> 114 97 112 101 103))))
looks like this is not compatible:
--
character designator n. a designator for a character; that is, an object
that denotes a character and that is one of: a designator for a string of
length one (denoting the character that is its only element), or a character
(denoting itself).
--
it doesn't work in Lispworks(however it works in clisp and allegrcl).
)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
(prin1 "Jane dates only Lisp programmers"))
"Alex Mizrahi" <·········@xhotmail.com> writes:
> CV> (concatenate 'string (mapcar
> CV> #'character (reverse '(109 111
> CV> 99 46 111 111 104 97 121 64 118 99 100
> CV> 114 97 112 101 103))))
>
> looks like this is not compatible:
> --
> character designator n. a designator for a character; that is, an object
> that denotes a character and that is one of: a designator for a string of
> length one (denoting the character that is its only element), or a character
> (denoting itself).
> --
>
> it doesn't work in Lispworks(however it works in clisp and allegrcl).
Yes, indeed! SBCL fails on this also, as well it should: the HyperSpec
explicitly says: "(character 65.) is an error." My .signature now uses
code-char. It looks like CLISP, Allegro CL, and OpenMCL break ANSI
compliance in a very user-friendly manner :-)
--
Regards, (concatenate
Constantine Vetoshev 'string (mapcar #'code-char
(reverse '(109 111 99 46 111 111 104
97 121 64 118 99 100 114 97 112 101 103))))