Page 1 of 1

Question on methods and setting

Posted: Tue Aug 06, 2013 6:20 am
by sabra.crolleton
I'm feeling particularly stupid today. Have a method which specifies on numbers and arrays. When applied to accessors of an clos object, it works on arrays, but not on numbers. The question is why. I would think that passing by value would mean both fail. I would think that passing by reference, both would work. I'm missing something fundamental here.

Code: Select all

(defclass t3 ()
  ((a :accessor a :initarg :a :initform 1)
   (b :accessor b :initarg :b :initform (make-array 5 :initial-element 0))))

(defmethod add-something ((x number) y) (setf x (+ x y)))

(defmethod add-something ((x array) y) (setf (aref x 0) (+ (aref x 0) y)))

(let ((z (make-instance 't3)))
  (describe z)
  (add-something (a z) 21)
  (add-something (b z) 22)
  (describe z))

#<T3 {1008FAEC43}>
  [standard-object]

Slots with :INSTANCE allocation:
  A  = 1
  B  = #(0 0 0 0 0)
#<T3 {1008FAEC43}>
  [standard-object]

Slots with :INSTANCE allocation:
  A  = 1
  B  = #(22 0 0 0 0)
Question: why does (b z) get set but (a z) does not get set?

Re: Question on methods and setting

Posted: Tue Aug 06, 2013 6:54 am
by Kompottkin
Lisp is call-by-value, not call-by-reference. In

Code: Select all

(defmethod add-something ((x number) y) (setf x (+ x y)))
the variable x is local to the method. When the method is called, (a pointer to) the argument object is copied into the x variable. There is no way for add-something to modify whatever the place the supplied value originates from is.

Re: Question on methods and setting

Posted: Tue Aug 06, 2013 7:05 am
by sabra.crolleton
why does doing the same thing with the array work?

Re: Question on methods and setting

Posted: Tue Aug 06, 2013 1:38 pm
by Kompottkin
Because Lisp always passes references to objects (by value). Therefore, you can modify objects passed to you, but not the references to those objects. Your array method modifies the passed vector, which is an effect that is visible to the caller. Your number method modifies a locally bound reference to the passed number, not the number itself (which cannot be modified at all).

Re: Question on methods and setting

Posted: Wed Aug 07, 2013 7:57 am
by Goheeca
It looks like a bad approach. Why want you to do that? The special expressions places are different from a variable therefore this behaviour. Maybe I'll come up with a solution based on references, it needs create a reference datatype based on a referee, not an ultimate one.

Re: Question on methods and setting

Posted: Wed Aug 07, 2013 10:22 pm
by sabra.crolleton
Why was I asking? I am running some simulation models with different degrees of analysis. In the most simplistic model, the clos slots are merely ratios. Each more detailed level of analysis adds another dimension of data. Thus the slots of the clos objects in level 2 contain vectors, the slots in level 3 contain 2 dimensional arrays, the slots in level 4 contain 3 dimensional arrays. Generic methods allow me to call the same method names, but the data manipulation would be appropriate for the different levels of analysis. I actually started with levels 2-4, where everything worked,then was asked to create an introductory level without really thinking that the same process would not work if the slot only contained a number and not an object. The example posted was the simplest example I could create to show my lack of understanding.

Re: Question on methods and setting

Posted: Thu Aug 08, 2013 5:55 am
by Kompottkin
sabra.crolleton wrote:Each more detailed level of analysis adds another dimension of data. Thus the slots of the clos objects in level 2 contain vectors, the slots in level 3 contain 2 dimensional arrays, the slots in level 4 contain 3 dimensional arrays.
So the first level ought to contain 0-dimensional arrays. :)

Code: Select all

(let ((a (make-array '())))
  (setf (aref a) 100)
  (incf (aref a))
  (aref a))
;=> 101

Re: Question on methods and setting

Posted: Thu Aug 08, 2013 6:14 am
by Goheeca
Yeah, from a mathematical point of view the 0D array is a nice solution.

Re: Question on methods and setting

Posted: Thu Aug 08, 2013 1:51 pm
by sylwester
Wouldn't it be better to make a struct? Perhaps like Scheme SRFI-111 box?

Re: Question on methods and setting

Posted: Fri Aug 09, 2013 4:30 pm
by sabra.crolleton
I like the 0 dimensional array idea. It makes everything mathematically consistent in my mind.

Thank you.