Hey guys!
Basically, what I need is a way to change an objects properties from
within itself. (I'm pretty new to both oo and lisp, fyi :-))
This is was my attempt at doing something similair, but it doesn't
work at all.
Thanks for your time.
(defclass Submarine ()
((name :accessor sub-name
:initarg :sub-name)
(color :accessor sub-color
:initarg :sub-color)
(script :accessor sub-script
:initarg :sub-script
:initform '())))
(defclass Yellow-Submarine (Submarine)
()
(:default-initargs
:sub-name "Beatles"
:sub-color 'Yellow
:sub-script (list #'(lambda ()
(Change-color 'Green)))))
(defmethod Change-color
((self Yellow-Submarine)
(color symbol))
(setf (sub-color self) color))
(defun test ()
(let ((our-sub (make-instance 'yellow-submarine)))
(funcall (first (sub-script our-sub)))))
In article
<····································@e25g2000prg.googlegroups.com>,
········@gmail.com wrote:
> Hey guys!
>
> Basically, what I need is a way to change an objects properties from
> within itself. (I'm pretty new to both oo and lisp, fyi :-))
>
> This is was my attempt at doing something similair, but it doesn't
> work at all.
> Thanks for your time.
>
>
> (defclass Submarine ()
> ((name :accessor sub-name
> :initarg :sub-name)
> (color :accessor sub-color
> :initarg :sub-color)
> (script :accessor sub-script
> :initarg :sub-script
> :initform '())))
>
> (defclass Yellow-Submarine (Submarine)
> ()
> (:default-initargs
> :sub-name "Beatles"
> :sub-color 'Yellow
> :sub-script (list #'(lambda ()
> (Change-color 'Green)))))
>
> (defmethod Change-color
> ((self Yellow-Submarine)
> (color symbol))
> (setf (sub-color self) color))
>
> (defun test ()
> (let ((our-sub (make-instance 'yellow-submarine)))
> (funcall (first (sub-script our-sub)))))
CHANGE-COLOR is defined to have two parameters.
You are calling it with only one: (change-color 'green) .
Also note, that there is not really a 'within', since
Generic Functions are defined 'outside' of classes.
So you need to add a parameter to the function:
(defclass Yellow-Submarine (Submarine)
()
(:default-initargs
:sub-name "Beatles"
:sub-color 'Yellow
:sub-script (list #'(lambda (submarine)
(Change-color submarine 'Green)))))
Then you need to FUNCALL the function with the parameter:
(defun test ()
(let ((our-sub (make-instance 'yellow-submarine)))
(funcall (first (sub-script our-sub))
our-sub)))
--
http://lispm.dyndns.org/
On 6 Dec, 18:59, Rainer Joswig <······@lisp.de> wrote:
> In article
> <····································@e25g2000prg.googlegroups.com>,
>
>
>
> ········@gmail.com wrote:
> > Hey guys!
>
> > Basically, what I need is a way to change an objects properties from
> > within itself. (I'm pretty new to both oo and lisp, fyi :-))
>
> > This is was my attempt at doing something similair, but it doesn't
> > work at all.
> > Thanks for your time.
>
> > (defclass Submarine ()
> > ((name :accessor sub-name
> > :initarg :sub-name)
> > (color :accessor sub-color
> > :initarg :sub-color)
> > (script :accessor sub-script
> > :initarg :sub-script
> > :initform '())))
>
> > (defclass Yellow-Submarine (Submarine)
> > ()
> > (:default-initargs
> > :sub-name "Beatles"
> > :sub-color 'Yellow
> > :sub-script (list #'(lambda ()
> > (Change-color 'Green)))))
>
> > (defmethod Change-color
> > ((self Yellow-Submarine)
> > (color symbol))
> > (setf (sub-color self) color))
>
> > (defun test ()
> > (let ((our-sub (make-instance 'yellow-submarine)))
> > (funcall (first (sub-script our-sub)))))
>
> CHANGE-COLOR is defined to have two parameters.
> You are calling it with only one: (change-color 'green) .
>
> Also note, that there is not really a 'within', since
> Generic Functions are defined 'outside' of classes.
>
> So you need to add a parameter to the function:
>
> (defclass Yellow-Submarine (Submarine)
> ()
> (:default-initargs
> :sub-name "Beatles"
> :sub-color 'Yellow
> :sub-script (list #'(lambda (submarine)
> (Change-color submarine 'Green)))))
>
> Then you need to FUNCALL the function with the parameter:
>
> (defun test ()
> (let ((our-sub (make-instance 'yellow-submarine)))
> (funcall (first (sub-script our-sub))
> our-sub)))
>
> --http://lispm.dyndns.org/
Thanks
On Dec 6, 6:40 pm, ········@gmail.com wrote:
> Hey guys!
>
> Basically, what I need is a way to change an objects properties from
> within itself. (I'm pretty new to both oo and lisp, fyi :-))
>
> This is was my attempt at doing something similair, but it doesn't
> work at all.
> Thanks for your time.
>
> (defclass Submarine ()
> ((name :accessor sub-name
> :initarg :sub-name)
> (color :accessor sub-color
> :initarg :sub-color)
> (script :accessor sub-script
> :initarg :sub-script
> :initform '())))
>
> (defclass Yellow-Submarine (Submarine)
> ()
> (:default-initargs
> :sub-name "Beatles"
> :sub-color 'Yellow
> :sub-script (list #'(lambda ()
> (Change-color 'Green)))))
:sub-script (list #'(lambda (this) (change-color this 'green)))
Also note the naming style.
Cheers
--
Marco