This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Question on methods and setting

10 posts · 8027 views

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.
(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?

Last edited by sabra.crolleton on , edited 1 time in total.

Re: Question on methods and setting

Lisp is call-by-value, not call-by-reference. In
(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

why does doing the same thing with the array work?

Re: Question on methods and setting

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

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.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Question on methods and setting

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

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. :)
(let ((a (make-array '())))
  (setf (aref a) 100)
  (incf (aref a))
  (aref a))
;=> 101

Re: Question on methods and setting

Yeah, from a mathematical point of view the 0D array is a nice solution.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Question on methods and setting

Wouldn't it be better to make a struct? Perhaps like Scheme SRFI-111 box?
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: Question on methods and setting

I like the 0 dimensional array idea. It makes everything mathematically consistent in my mind.

Thank you.