From: ········@gmail.com
Subject: Having an object change itself
Date: 
Message-ID: <f2b72f20-4d2d-4254-bd85-a65c796396ff@e25g2000prg.googlegroups.com>
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)))))

From: Rainer Joswig
Subject: Re: Having an object change itself
Date: 
Message-ID: <joswig-5CA3E4.18593406122007@news-europe.giganews.com>
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/
From: ········@gmail.com
Subject: Re: Having an object change itself
Date: 
Message-ID: <d4e75e6c-228a-4f2a-95ef-0d0e36a22457@p69g2000hsa.googlegroups.com>
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
From: Marco Antoniotti
Subject: Re: Having an object change itself
Date: 
Message-ID: <90d8246b-b7a9-4fad-b607-1dd893bf1116@i29g2000prf.googlegroups.com>
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